包含多种视图类型的Recyclerview

时间:2015-12-26 07:01:29

标签: android android-recyclerview

我创建了一个包含多种视图类型的recyclerview。第一个问题是图像不显示,第二个问题是当我滚动recyclelerview时, 其项目的价值发生变化,我不知道如何解决它。 我的适配器代码:

public class HeterogenousRecyclerviewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private ArrayList<DataObject> mDataset;
    Context context;
    RecyclerView.ViewHolder viewHolder;


    public HeterogenousRecyclerviewAdapter(ArrayList<DataObject> myDataset) {
        this.mDataset = myDataset;
    }

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

    @Override
    public int getItemViewType(int position) {
        int view_type=mDataset.get(position).getView_type();
        return view_type;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        context=parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());

        switch (viewType){
            case 0:
                View v1 = inflater.inflate(R.layout.layout_viewholder1, parent, false);
                viewHolder = new ViewHolder1(v1);
                break;
            case 1:
                View v2 = inflater.inflate(R.layout.layout_viewholder2, parent, false);
                viewHolder = new ViewHolder2(v2);
                break;
        }

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        switch (viewHolder.getItemViewType()){
            case 0:
                ViewHolder1 vh1 = (ViewHolder1) viewHolder;
                configureViewHolder1(vh1, position);
                break;
            case 1:
                ViewHolder2 vh2 = (ViewHolder2) viewHolder;
                configureViewHolder2(vh2, position);
                break;
        }

    }

    private void configureViewHolder1(ViewHolder1 vh1, int position) {
        if (mDataset != null) {
            vh1.getLabel1().setText(mDataset.get(position).getName());
            //vh1.getLabel2().setText("Hometown: " + user.hometown);
        }
    }

    private void configureViewHolder2(ViewHolder2 vh2, int position) {

        //vh2.getImageView().setImageResource(R.mipmap.img1);
        try {
            Resources res = context.getResources();
            int resourceId = res.getIdentifier(mDataset.get(position).getImg(), "mipmap",
                    context.getPackageName());
            vh2.getImageView().setImageResource(resourceId);
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
}

这是我的ViewHolder1类:

public class ViewHolder1 extends RecyclerView.ViewHolder {

    private TextView label1;

    public ViewHolder1(View v) {
        super(v);
        label1 = (TextView) v.findViewById(R.id.text1);
        //label2 = (TextView) v.findViewById(R.id.text2);
    }

    public TextView getLabel1() {
        return label1;
    }

    public void setLabel1(TextView label1) {
        this.label1 = label1;
    }

}

这是ViewHolder1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:textStyle="bold" />

</LinearLayout>

这是ViewHolder2类:

public class ViewHolder2 extends RecyclerView.ViewHolder {

    private ImageView ivExample;

    public ViewHolder2(View v) {
        super(v);
        ivExample = (ImageView) v.findViewById(R.id.ivExample);
    }

    public ImageView getImageView() {
        return ivExample;
    }

    public void setImageView(ImageView ivExample) {
        this.ivExample = ivExample;
    }
}

这是ViewHolder2.xml:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ivExample"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    android:layout_width="200dp"
    android:layout_height="200dp"/>

这是我的主要活动代码:

        ArrayList<DataObject> personList = new ArrayList<DataObject>();
        DataObjectDBAdapter dataObjectDBAdapter = new     DataObjectDBAdapter(getApplicationContext());
        personList = dataObjectDBAdapter.getALL();
        adapter = new HeterogenousRecyclerviewAdapter(personList);

        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
        mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(adapter);

能帮帮我吗?

3 个答案:

答案 0 :(得分:1)

我没有在SQLite数据库中保存图像的名称,所以第一个问题就是因为这个,第二个问题出现在我的适配器代码中。我不得不写&#34; viewHolder&#34;而不是&#34;持有人&#34;在onBindViewHolder()中,所以正确的方法是:

@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {

    switch (viewHolder.getItemViewType()){
        case 0:
            ViewHolder1 vh1 = (ViewHolder1) viewHolder;
            configureViewHolder1(vh1, position);
            break;
        case 1:
            ViewHolder2 vh2 = (ViewHolder2) viewHolder;
            configureViewHolder2(vh2, position);
            break;
    }

}

答案 1 :(得分:0)

  

首先确保给定的语句必须只返回0或1.因为视图类型必须以0开头

int view_type=mDataset.get(position).getView_type();

无需在viewHolder

的成员中创建以下class
RecyclerView.ViewHolder viewHolder;

请勿在{{1​​}}中查看viewType。而是检查持有者instanceOf

onBindViewHolder

答案 2 :(得分:0)

  

在滚动后,我的问题值更改相同,所以我覆盖了这些方法

@Override
public long getItemId(int position) {
    return position;
}

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

这解决了我的问题,并且对于Image heck,image resourceId是否为null    我不确定您设置资源的方式是否合适。