当前位置:  开发笔记 > Android > 正文

ListView与水平ScrollView

如何解决《ListView与水平ScrollView》经验,为你挑选了1个好方法。

我正在尝试创建一个列表视图,其中包含一个水平滚动视图作为每一行.我希望元素垂直排列,如果有超过一定数量的项目,视图将变为可滚动.

然而它看起来像这样.

在此输入图像描述

我正在膨胀以下的xml

single_row.xml




    

        

        
        

    


这是我将视图添加到视图的适配器

Adapter.java

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.example.jj.library.Chip;
import com.example.jj.library.ChipView;
import com.example.jj.library.ChipViewAdapter;
import com.example.jj.library.OnChipClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jj on 12/21/2015.
 */
public class LiveFeedAdapter extends ArrayAdapter implements OnChipClickListener {


    private static final String TAG = "LIVEFEED ADAPTER";
    Context CTX;
    private ChipView mTextChipLayout;
    public List liveFeed_list = new ArrayList();

    public LiveFeedAdapter(Context context, int resource) {
        super(context, resource);
        CTX = context;
    }

    @Override
    public void add(LiveFeedDataProvider object){
        liveFeed_list.add(object);
        super.add(object);
    }

    @Override
    public int getCount() {
        return liveFeed_list.size();
    }


    @Override
    public LiveFeedDataProvider getItem(int position) {

        return liveFeed_list.get(position);
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        if(convertView == null){
            LayoutInflater inflator = (LayoutInflater) CTX.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflator.inflate(R.layout.single_livefeed_row,parent,false);
        }
        ChipViewAdapter adapterLayout = new MainChipViewAdapter(getContext());
        mTextChipLayout = (ChipView) convertView.findViewById(R.id.text_chip_layout);
        mTextChipLayout.setAdapter(adapterLayout);
        mTextChipLayout.setChipLayoutRes(R.layout.chip_close);
        mTextChipLayout.setChipBackgroundColor(CTX.getResources().getColor(R.color.light_blue));
        mTextChipLayout.setChipBackgroundColorSelected(CTX.getResources().getColor(R.color.green));
        mTextChipLayout.setOnChipClickListener(this);
        LiveFeedDataProvider provider = liveFeed_list.get(position);
        Log.d(TAG, "LENGTH = " + provider.interests.length);
        for(int i = 0; i < provider.interests.length; i++) {
            String interest = provider.interests[i];
            mTextChipLayout.add(new Tag(interest));
        }
            return convertView;
    }

    @Override
    public void onChipClick(Chip chip) {

    }

}

Konstantin L.. 16

我解决你的问题有点不同的方式:我取代HorizontalScrollViewListView通过RecyclerView与各行的RecyclerView,正如我相信,在这里优选的布局.

这是一个结果:

在此输入图像描述

(不要太注意丑陋的设计:-))

MainActivity 布局



MainActivity 类:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RecyclerView chipRecyclerView = (RecyclerView)findViewById(R.id.recyclerView);
        chipRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        ArrayList chipsArray = new ArrayList<>();
        chipsArray.add(new String[] {"Fitness", "Gaming", "Education","Animals", "Cars"});
        .....
        chipsArray.add(new String[] {"Education","Animals", "Cars"});

        chipRecyclerView.setAdapter(new ListChipsAdapter(chipsArray));
    }
}

ListChipsAdapter- 适用于MainActivity回收者视图的适配器.它管理带有芯片的行列表.

public class ListChipsAdapter extends RecyclerView.Adapter {

    private ArrayList chipsArray;

    public ListChipsAdapter(ArrayList chipsArray) {
        this.chipsArray = chipsArray;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ChipsViewHolder(new RowChipsView(parent.getContext()));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((RowChipsView)holder.itemView).setAdapter(new ChipsAdapter(chipsArray.get(position)));
    }

    @Override
    public int getItemCount() {
        return chipsArray.size();
    }

    private class ChipsViewHolder extends RecyclerView.ViewHolder {

        public ChipsViewHolder(View itemView) {
            super(itemView);
        }
    }
}

RowChipsView - 是一个代表特定单行的类:

public class RowChipsView extends FrameLayout {

    public RowChipsView(Context context) {
        super(context);
        initializeView(context);
    }

    private void initializeView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.single_row, this);
        ((RecyclerView)findViewById(R.id.recyclerViewHorizontal)).setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
    }

    public void setAdapter(ChipsAdapter adapter) {
        ((RecyclerView)findViewById(R.id.recyclerViewHorizontal)).setAdapter(adapter);
    }
}

它的布局只包含一个RecyclerView:



现在,ChipsAdapter- 每一行使用的适配器:

public class ChipsAdapter extends RecyclerView.Adapter {

    private String[] chipsArray;

    public ChipsAdapter(String[] chipsArray) {
        this.chipsArray = chipsArray;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ChipViewHolder(new ChipView(parent.getContext()));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((ChipView)holder.itemView).displayItem(chipsArray[position]);
    }

    @Override
    public int getItemCount() {
        return chipsArray.length;
    }

    private class ChipViewHolder extends RecyclerView.ViewHolder {

        public ChipViewHolder(View itemView) {
            super(itemView);
        }
    }
}

现在,最后一块是ChipView:

public class ChipView extends FrameLayout {

    public ChipView(Context context) {
        super(context);
        initializeView(context);
    }

    private void initializeView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.chip_view, this);
    }

    public void displayItem(String text) {
        ((TextView)findViewById(R.id.chipTextView)).setText(text);
    }
}

它的布局:



    

我已将项目上传到我的Dropbox,所以请随时查看!

我希望,这有帮助.



1> Konstantin L..:

我解决你的问题有点不同的方式:我取代HorizontalScrollViewListView通过RecyclerView与各行的RecyclerView,正如我相信,在这里优选的布局.

这是一个结果:

在此输入图像描述

(不要太注意丑陋的设计:-))

MainActivity 布局



MainActivity 类:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RecyclerView chipRecyclerView = (RecyclerView)findViewById(R.id.recyclerView);
        chipRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        ArrayList chipsArray = new ArrayList<>();
        chipsArray.add(new String[] {"Fitness", "Gaming", "Education","Animals", "Cars"});
        .....
        chipsArray.add(new String[] {"Education","Animals", "Cars"});

        chipRecyclerView.setAdapter(new ListChipsAdapter(chipsArray));
    }
}

ListChipsAdapter- 适用于MainActivity回收者视图的适配器.它管理带有芯片的行列表.

public class ListChipsAdapter extends RecyclerView.Adapter {

    private ArrayList chipsArray;

    public ListChipsAdapter(ArrayList chipsArray) {
        this.chipsArray = chipsArray;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ChipsViewHolder(new RowChipsView(parent.getContext()));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((RowChipsView)holder.itemView).setAdapter(new ChipsAdapter(chipsArray.get(position)));
    }

    @Override
    public int getItemCount() {
        return chipsArray.size();
    }

    private class ChipsViewHolder extends RecyclerView.ViewHolder {

        public ChipsViewHolder(View itemView) {
            super(itemView);
        }
    }
}

RowChipsView - 是一个代表特定单行的类:

public class RowChipsView extends FrameLayout {

    public RowChipsView(Context context) {
        super(context);
        initializeView(context);
    }

    private void initializeView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.single_row, this);
        ((RecyclerView)findViewById(R.id.recyclerViewHorizontal)).setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
    }

    public void setAdapter(ChipsAdapter adapter) {
        ((RecyclerView)findViewById(R.id.recyclerViewHorizontal)).setAdapter(adapter);
    }
}

它的布局只包含一个RecyclerView:



现在,ChipsAdapter- 每一行使用的适配器:

public class ChipsAdapter extends RecyclerView.Adapter {

    private String[] chipsArray;

    public ChipsAdapter(String[] chipsArray) {
        this.chipsArray = chipsArray;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ChipViewHolder(new ChipView(parent.getContext()));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((ChipView)holder.itemView).displayItem(chipsArray[position]);
    }

    @Override
    public int getItemCount() {
        return chipsArray.length;
    }

    private class ChipViewHolder extends RecyclerView.ViewHolder {

        public ChipViewHolder(View itemView) {
            super(itemView);
        }
    }
}

现在,最后一块是ChipView:

public class ChipView extends FrameLayout {

    public ChipView(Context context) {
        super(context);
        initializeView(context);
    }

    private void initializeView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.chip_view, this);
    }

    public void displayItem(String text) {
        ((TextView)findViewById(R.id.chipTextView)).setText(text);
    }
}

它的布局:



    

我已将项目上传到我的Dropbox,所以请随时查看!

我希望,这有帮助.


请注意,如果您选择将RecyclerAdapter的ViewHolder类与RecyclerAdapter放在同一个文件中,则应将其声明为静态.否则你迟早会遇到内存问题.
推荐阅读
臭小子
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有