将recyclerview放入其他Listview

时间:2017-07-07 06:44:12

标签: android android-recyclerview android-adapter

enter image description here这是我在其他recyclerview中充气的文件,我还在其中添加了另一个recyclerview,但我无法设置它。

我也搜索了一些参考文献,但找不到合适的参考文献。请帮我这里怎么做。

谢谢。

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:layout_margin="10dp"
    android:elevation="3dp"
    app:cardBackgroundColor="#b7a2d6"
    app:cardCornerRadius="10dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/lesson_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="LessonName"
            android:textColor="#fff"
            android:textSize="25sp"
            android:textStyle="bold" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerViewChapters"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/lesson_name"
            android:padding="10dp" />
    </RelativeLayout>
</android.support.v7.widget.CardView>

SubjectActivity:

public class SubjectActivity extends AppCompatActivity {

    private RecyclerView lessonNamerecyclerView, recyclerViewChapters;


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

        lessonNamerecyclerView = (RecyclerView) findViewById(R.id.recyclerViewLesson);
        recyclerViewChapters = (RecyclerView) findViewById(R.id.recyclerViewChapters);

        lessonNamerecyclerView.setLayoutManager(new LinearLayoutManager(this));
        lessonNamerecyclerView.setAdapter(new LessonAdapter());

        recyclerViewChapters.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));
        recyclerViewChapters.setAdapter(new DemoVideoAdapter());


    }

}

1 个答案:

答案 0 :(得分:2)

我已经实现了类似的东西,我做了一个主要的listview而不是循环视图,并将水平的recycleview作为llistview的行。添加一些代码片段希望它有所帮助。

Main Activity - only simple list view
 listview= (ListView) findViewById(R.id.listView);
    ListAdapter adapter=new ListAdapter(this);
    listview.setAdapter(adapter);

List Adapter
public class ListAdapter extends BaseAdapter {
private Context context;


private int lengt=4;
private String[] names={"Item 1","Item 2","Item 3 ","Item 4","Item 5"};
LayoutInflater layoutInflater;
RecyclerAdapter recyclerAdapter;
public ListAdapter(Context context){
    this.context=context;
    layoutInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    recyclerAdapter=new RecyclerAdapter(context);
}



@Override
public int getCount() {
    return lengt;
}

@Override
public Object getItem(int i) {
    return null;
}

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

@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
    View view1=layoutInflater.inflate(R.layout.recyclelayout,null,false);
    // here was text view 
   // TextView tittle=(TextView)view1.findViewById(R.id.textView);
   // tittle.setText(names[i]);
   // TextView se=(TextView)view1.findViewById(R.id.textView2);
   // se.setText("See All >");
    RecyclerView recyclerView=(RecyclerView)view1.findViewById(R.id.recycler);
    recyclerView.setAdapter(recyclerAdapter);

    LinearLayoutManager linearLayoutManager=new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false);
    recyclerView.setLayoutManager(linearLayoutManager);
    return view1;
}

}

列表视图的xml行recyclelayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:clipToPadding="false">
<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:id="@+id/recycler">
</android.support.v7.widget.RecyclerView>

Recycleview适配器类就是这个。

class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerHolder> {
public int[] image={R.drawable.ic_close_24dp,R.drawable.ic_more_vert_24dp,R.drawable.ic_play_arrow_24dp,R.drawable.ic_skip_next_24dp};
private Context context;
public RecyclerAdapter(Context context){
    this.context=context;
}
@Override
public RecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclercontent,parent,false);
    RecyclerHolder vh=new RecyclerHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(RecyclerHolder holder, int position) {
    holder.imageView.setImageResource(image[position]);
}

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

public class RecyclerHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    public ImageView imageView;
    public CardView cardView;
    public RecyclerHolder(View itemView) {
        super(itemView);
        imageView=(ImageView)itemView.findViewById(R.id.imageView2);
        cardView=(CardView)itemView.findViewById(R.id.carview);
        imageView.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == imageView.getId()) {
            Toast.makeText(view.getContext(),"Item Pressed =" + String.valueOf(getAdapterPosition()),Toast.LENGTH_SHORT).show();
         //   Intent intent=new Intent(context,ItemInfo.class);
           // context.startActivity(intent);
        }
    }
}

}

It Looks Like this with each row have recycleview 有任何疑问只是让我知道。希望它可以帮到你。

编辑 - 添加剩余的xml文件

recyclercontent.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
android:outlineProvider="bounds">
<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:translationZ="10dp"
    android:id="@+id/carview">
    <ImageView
        android:layout_width="150dp"
        android:layout_height="145dp"
        android:id="@+id/imageView2"
        android:layout_marginLeft="0dp" />
</android.support.v7.widget.CardView>

swipelayout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/imageView"
    android:layout_gravity="center_vertical" />