如何在列表视图的项目中设置RecyclerView的项目的位置

时间:2018-08-23 12:09:05

标签: android listview android-recyclerview recycler-adapter

我有一个包含很多项目的Listview,每个项目都有两个textview和recyclerview。在recyclerview中,我有一个水平滚动的图像列表,但是问题是,当我得到recyclerview的json时,所有项目都显示在假设我在4个listview的recyclerview中有4个项目。每个listview的recyclerview中都有1个图像,但是在列表的每个项目中全部显示。

我要在列表视图中使用这种类型的项目。

     No:1                  Date:
     img1

     No:2                  Date:
     img2

     No:3                  Date:
     img3

     No:4                  Date:
     img4

我得到了我不想要的列表视图。

     No:1                  Date:
     img1,img2,img3,img4

     No:2                  Date:
     img1,img2,img3,img4

     No:3                  Date:
     img1,img2,img3,img4

     No:4                  Date:
     img1,img2,img3,img4

这是我的Listview适配器

public class POAdapter extends ArrayAdapter {

private Context mContext;

private RecyclerView.Adapter adapter;
private List list = new ArrayList();
PO details;
public POAdapter(@NonNull Context context, int resource) {
    super(context, resource);
    mContext=context;

}

public void add(PO object) {
    super.add(object);
    list.add(object);
}

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

@Nullable
@Override
public Object getItem(int position) {
    return list.get(position);
}


@Override
public boolean isEnabled(int position) {
    details=(PO)this.getItem(position);
    if(details.getHideStatus()==1){
        return false;
    }
    return true;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    View row;
    row = convertView;
    details= (PO) this.getItem(position);
    final DetailHolder detailHolder;
    if(row == null){
        LayoutInflater layoutInflater= (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row=layoutInflater.inflate(R.layout.polist_data_activity,parent,false);
        detailHolder = new DetailHolder();
        detailHolder.poNoTv = (TextView) row.findViewById(R.id.po_no_tv);
        detailHolder.deliveryDtTv = (TextView) row.findViewById(R.id.Delivery_date_tv);
        detailHolder.metersCompletedTv = (TextView) row.findViewById(R.id.total_meters_tv);
        detailHolder.recyclerView=(RecyclerView)row.findViewById(R.id.image_list);
        detailHolder.commentsTv = (TextView) row.findViewById(R.id.comment_tv);
        //detailHolder.totalMeters_tv= (TextView) row.findViewById(R.id.meter_completed_tv);
        row.setTag(detailHolder);
    }else{
         detailHolder = (DetailHolder)row.getTag();
    }


    LinearLayoutManager layoutManager
            = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
    detailHolder.recyclerView.setLayoutManager(layoutManager);
    System.out.println("DESIGNLIST" +((POListActivity)mContext).getDesignList());
    adapter= new ImageAdapter(((POListActivity)mContext).getDesignList(),getContext());
    detailHolder.recyclerView.setAdapter(adapter);




    //detailHolder.lstImages.setLayoutManager(layoutManager);
    detailHolder.poNoTv.setText(details.getPoNo());
    detailHolder.deliveryDtTv.setText(details.getDeliveryDt());
    DecimalFormat formatter = new DecimalFormat("#,###,###");
    try {
        //compMeters = (formatter.format(details.getMetersCompleted()));
    }catch (NumberFormatException e){}


    try {
       // totalMeters = (formatter.format(details.getTotalMeters()));
    }catch (NumberFormatException e){}
   // detailHolder.metersCompletedTv.setText(compMeters+" / "+totalMeters+" Meters");
 //   detailHolder.totalMeters_tv.setText(totalMeters);

    //row.setBackgroundColor(position % 2 == 0 ? ContextCompat.getColor(this.getContext(), R.color.fst_color) : ContextCompat.getColor(this.getContext(), R.color.scnd_color));

    return row;

}



static class DetailHolder {
    TextView poNoTv, deliveryDtTv, metersCompletedTv, commentsTv;
    RecyclerView recyclerView;
}
}

这是recyclerview的适配器

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

private List<Design> horizontalDesignList;
Context context;

public ImageAdapter(List<Design> DesignList, Context context) {
    this.horizontalDesignList= DesignList;
    this.context = context;
}


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

@Override
public int getItemViewType(int position) {
    return position;
}

@NonNull
@Override
public ImageAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View designsView = LayoutInflater.from(parent.getContext()).inflate(R.layout.po_image_data, parent, false);
    ViewHolder designsHolder = new ViewHolder(designsView);
    return designsHolder;
}

@Override
public void onBindViewHolder(@NonNull ImageAdapter.ViewHolder holder, int position) {
  //  holder.imageView.setImageResource(horizontalDesignList.get(position).getImgName());
    Picasso.with(context).load(horizontalDesignList.get(position).getBasePath()+horizontalDesignList.get(position).getImgName())
            .into(holder.imageView);
    int workStatus=horizontalDesignList.get(position).getWorkStatus();
    holder.imageView.setBorderWidth(4);
    if(workStatus==1){
        holder.imageView.setBorderColor(Color.GREEN);
    }else if(workStatus == 2){
        holder.imageView.setBorderColor(Color.YELLOW);
    }else if(workStatus==3){
        holder.imageView.setBorderColor(Color.RED);
    }else {
      //  Toast.makeText(context, "No Work Status", Toast.LENGTH_SHORT).show();
    }
    final Design current = (Design) horizontalDesignList.get(position);
    holder.imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, PODesignDetailsActivity.class);
            intent.putExtra("poDesignId",current.getPoDesignId());
            context.startActivity(intent);
        }
    });
    holder.textDesign.setText(horizontalDesignList.get(position).getDesignColor());
}

@Override
public int getItemCount() {
    return horizontalDesignList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder{

    public CircleImageView imageView;
    public TextView textDesign;

    public ViewHolder(View v){

        super(v);

        imageView = (CircleImageView) v.findViewById(R.id.img);
        textDesign= (TextView)v.findViewById(R.id.design_color_tv);
    }
}

}

请给我解释,帮助我,指导我。

1 个答案:

答案 0 :(得分:0)

使用下面的示例来实现您想要的:

1)Demo2.class:---------------

public class Demo2 extends AppCompatActivity {

private ListView lv;
private Adapter adapter;
private List<String> items;
private List<List<String>> items_inside;

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

    items = new ArrayList<>(); // one dimensional array

    // Example: index 0 --> A1
    //          index 1 --> A2
    //          ...

    items_inside = new ArrayList<>(); // 2 dimensional array

    // Example: index 0 --> {A1B1 , A1B2 , ...}
    //          index 1 --> {A2B1 , A2B2 , ...}
    //          ...

    // populate items and items_inside
    for(int i = 0 ; i< 4 ; i++){
        List<String> tmp = new ArrayList<>();
        items.add( i , "A" + (i+1));
        for(int j = 0 ; j < 4 ; j++){
            tmp.add(j , "A" + (i+1) + "B" + (j+1));
        }
        items_inside.add(i , tmp);
    }

    lv = (ListView) findViewById(R.id.lv);
    adapter = new Adapter(getApplicationContext() , items , items_inside);
    lv.setAdapter(adapter);

}
}

2)Adapter.class:---------

public class Adapter extends BaseAdapter {

private Context context;
private LayoutInflater layoutInflater;
private List<String> items;
private List<List<String>> items_inside;

public Adapter(Context context , List<String> items  , List<List<String>> items_inside) {

    this.context = context;

    layoutInflater = LayoutInflater.from(context);

    this.items = items;

    this.items_inside = items_inside;

}


public int getCount() {
    return items.size();
}

public Object getItem(int position) {
    return items.get(position);
}

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

public View getView(final int position, View convertView, ViewGroup parent) {

    View view = convertView;
    final TextView tv_list_item;
    final ListView lv_inner;

    if (convertView == null) {
        if (layoutInflater != null) {
            view = layoutInflater.inflate(R.layout.list_item, null);
        }
    }

    tv_list_item = (TextView) view.findViewById(R.id.tv_list_item);
    tv_list_item.setText(items.get(position));

    lv_inner = (ListView) view.findViewById(R.id.lv_inner);
    lv_inner.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView absListView, int i) {

        }

        @Override
        public void onScroll(AbsListView absListView, int i, int i1, int i2) {
            lv_inner.getParent().requestDisallowInterceptTouchEvent(true);
        }
    });
    InnerAdapter innerAdapter = new InnerAdapter(context , items_inside.get(position));
    lv_inner.setAdapter(innerAdapter);

    return view;
}
}

3)InnerAdapter.class:------

public class InnerAdapter extends BaseAdapter {


private Context context;
private LayoutInflater layoutInflater;
private List<String> items_inside;

public InnerAdapter(Context context, List<String> items_inside) {

    this.context = context;

    layoutInflater = LayoutInflater.from(context);

    this.items_inside = items_inside;

}


public int getCount() {
    return items_inside.size();
}

public Object getItem(int position) {
    return items_inside.get(position);
}

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

public View getView(final int position, View convertView, ViewGroup parent) {

    View view = convertView;
    final TextView tv_inner;

    if (convertView == null) {
        if (layoutInflater != null) {
            view = layoutInflater.inflate(R.layout.list_items_inner, null);
        }
    }

    tv_inner = (TextView) view.findViewById(R.id.tv_inner);
    tv_inner.setText(items_inside.get(position));

    return view;
}
}

4)demo2.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:weightSum="100"
android:orientation="vertical">

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/lv"
    android:layout_marginStart="10dp"
    android:layout_marginEnd="10dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp">
</ListView>

</LinearLayout>

5)list_items.xml:-------------

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:layout_height="75dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:id="@+id/ll"
    android:weightSum="100"
    android:orientation="horizontal">

<TextView
    android:id="@+id/tv_list_item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="50"
    android:layout_marginStart="10dp"
    android:gravity="start"
    android:text="A" />

    <TextView
        android:id="@+id/tv_date"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginEnd="4dp"
        android:gravity="center"
        android:layout_weight="50"
        android:text="Date:" />

</LinearLayout>

<ListView
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:layout_alignParentBottom="true"
    android:layout_below="@id/ll"
    android:id="@+id/lv_inner">
</ListView>

</RelativeLayout>

6)list_items_inner.xml:--------

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_marginEnd="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginStart="10dp"
android:layout_height="match_parent">

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="AB"
    android:gravity="start"
    android:id="@+id/tv_inner"/>

</android.support.constraint.ConstraintLayout>

7)输出:---------

enter image description here