gpt4 book ai didi

android - 将recyclerview放在其他Listview中

转载 作者:行者123 更新时间:2023-11-29 19:12:10 25 4
gpt4 key购买 nike

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>

主题 Activity :

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());


}

}

最佳答案

我已经实现了类似的东西,我制作了一个主 ListView 而不是回收 View ,并将水平回收 View 制作为 llistview 的行。添加一些代码 fragment 希望对您有所帮助。

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;
}

recyclelayout ListView 行的 xml

<?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" />

关于android - 将recyclerview放在其他Listview中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44964192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com