参见英文答案 > How to build a Horizontal ListView with RecyclerView? 13个
我学习机器人,我似乎无法找到一种方法来做到这一点:
我想做左侧滚动的权利,我猜是一个recylcer视图,但不确定它是否这样.我只需要有人指出我正确的方向.
我想我需要一个RecyclervIEw的内部布局.
如果有人能指出我在哪里搞清楚这一点,我会非常感激!
解决方法:
第1步
创建两个模型类,如下所示.
package-name-here;public class SingleItemmodel { private String name; private String url; private String description; public SingleItemmodel() { } public SingleItemmodel(String name, String url) { this.name = name; this.url = url; } public String getUrl() { return url; } public voID setUrl(String url) { this.url = url; } public String getname() { return name; } public voID setname(String name) { this.name = name; } public String getDescription() { return description; } public voID setDescription(String description) { this.description = description; }}
>创建其他模型类SectionDataModel.java
package-name-here;;import java.util.ArrayList;public class SectionDataModel {private String headerTitle;private ArrayList<SingleItemmodel> allitemsInSection;public SectionDataModel() {}public SectionDataModel(String headerTitle, ArrayList<SingleItemmodel> allitemsInSection) { this.headerTitle = headerTitle; this.allitemsInSection = allitemsInSection;}public String getheaderTitle() { return headerTitle;}public voID setheaderTitle(String headerTitle) { this.headerTitle = headerTitle;}public ArrayList<SingleItemmodel> getAllitemsInSection() { return allitemsInSection;}public voID setAllitemsInSection(ArrayList<SingleItemmodel> allitemsInSection) { this.allitemsInSection = allitemsInSection;}}
3.步骤:2
使用RecyclerVIEw创建活动以按垂直顺序显示列表.
package-name-here;import androID.os.Bundle;import androID.support.v7.app.AppCompatActivity;import androID.support.v7.Widget.linearlayoutmanager;import androID.support.v7.Widget.RecyclerVIEw;import androID.support.v7.Widget.Toolbar;import com.pratap.gplaystore.adapters.RecyclerVIEwDataAdapter;import com.pratap.gplaystore.models.SectionDataModel;import com.pratap.gplaystore.models.SingleItemmodel;import java.util.ArrayList;public class MainActivity extends AppCompatActivity { private Toolbar toolbar; ArrayList<SectionDataModel> allSampleData; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); toolbar = (Toolbar) findVIEwByID(R.ID.toolbar); allSampleData = new ArrayList<SectionDataModel>(); if (toolbar != null) { setSupportActionbar(toolbar); toolbar.setTitle("G PlayStore"); } createDummyData(); RecyclerVIEw my_recycler_vIEw = (RecyclerVIEw) findVIEwByID(R.ID.my_recycler_vIEw); my_recycler_vIEw.setHasFixedSize(true); RecyclerVIEwDataAdapter adapter = new RecyclerVIEwDataAdapter(this, allSampleData); my_recycler_vIEw.setLayoutManager(new linearlayoutmanager(this, linearlayoutmanager.VERTICAL, false)); my_recycler_vIEw.setAdapter(adapter); } public voID createDummyData() { for (int i = 1; i <= 5; i++) { SectionDataModel dm = new SectionDataModel(); dm.setheaderTitle("Section " + i); ArrayList<SingleItemmodel> singleItem = new ArrayList<SingleItemmodel>(); for (int j = 0; j <= 5; j++) { singleItem.add(new SingleItemmodel("Item " + j, "URL " + j)); } dm.setAllitemsInSection(singleItem); allSampleData.add(dm); } }}
>为上述活动类创建XML布局
activity_main.xml中
<?xml version="1.0" enCoding="utf-8"?><linearLayout 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="match_parent" androID:orIEntation="vertical"> <androID.support.v7.Widget.Toolbar androID:ID="@+ID/toolbar" androID:layout_wIDth="match_parent" androID:layout_height="?attr/actionbarSize" androID:background="?attr/colorPrimary" androID:elevation="8dp" androID:theme="@style/themeOverlay.AppCompat.Dark.Actionbar" app:popuptheme="@style/themeOverlay.AppCompat.light" /> <androID.support.v7.Widget.RecyclerVIEw androID:ID="@+ID/my_recycler_vIEw" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:scrollbars="none" /></linearLayout>
步骤:3
现在为MainActivity中的recyclerVIEw创建一个Adapter类.
RecyclerVIEwDataAdapter.java
package-name-here.adapters;import androID.content.Context;import androID.support.v7.Widget.linearlayoutmanager;import androID.support.v7.Widget.RecyclerVIEw;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.button;import androID.Widget.TextVIEw;import androID.Widget.Toast;import com.pratap.gplaystore.R;import com.pratap.gplaystore.models.SectionDataModel;import java.util.ArrayList;public class RecyclerVIEwDataAdapter extends RecyclerVIEw.Adapter<RecyclerVIEwDataAdapter.ItemRowHolder> { private ArrayList<SectionDataModel> dataList; private Context mContext; public RecyclerVIEwDataAdapter(Context context, ArrayList<SectionDataModel> dataList) { this.dataList = dataList; this.mContext = context; } @OverrIDe public ItemRowHolder onCreateVIEwHolder(VIEwGroup vIEwGroup, int i) { VIEw v = LayoutInflater.from(vIEwGroup.getContext()).inflate(R.layout.List_item, null); ItemRowHolder mh = new ItemRowHolder(v); return mh; } @OverrIDe public voID onBindVIEwHolder(ItemRowHolder itemRowHolder, int i) { final String sectionname = dataList.get(i).getheaderTitle(); ArrayList singleSectionItems = dataList.get(i).getAllitemsInSection(); itemRowHolder.itemTitle.setText(sectionname); SectionListDataAdapter itemListDataAdapter = new SectionListDataAdapter(mContext, singleSectionItems); itemRowHolder.recycler_vIEw_List.setHasFixedSize(true); itemRowHolder.recycler_vIEw_List.setLayoutManager(new linearlayoutmanager(mContext, linearlayoutmanager.HORIZONTAL, false)); itemRowHolder.recycler_vIEw_List.setAdapter(itemListDataAdapter); itemRowHolder.btnMore.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { Toast.makeText(v.getContext(), "click event on more, "+sectionname , Toast.LENGTH_SHORT).show(); } }); /* GlIDe.with(mContext) .load(FeedItem.getimageURL()) .diskCacheStrategy(diskCacheStrategy.ALL) .centerCrop() .error(R.drawable.bg) .into(FeedListRowHolder.thumbVIEw);*/ } @OverrIDe public int getItemCount() { return (null != dataList ? dataList.size() : 0); } public class ItemRowHolder extends RecyclerVIEw.VIEwHolder { protected TextVIEw itemTitle; protected RecyclerVIEw recycler_vIEw_List; protected button btnMore; public ItemRowHolder(VIEw vIEw) { super(vIEw); this.itemTitle = (TextVIEw) vIEw.findVIEwByID(R.ID.itemTitle); this.recycler_vIEw_List = (RecyclerVIEw) vIEw.findVIEwByID(R.ID.recycler_vIEw_List); this.btnMore= (button) vIEw.findVIEwByID(R.ID.btnMore); } }}
>现在为上述适配器类创建一个xml布局文件.
List_item.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="wrap_content" androID:background="?androID:selectableItemBackground" androID:orIEntation="vertical" androID:padding="5dp"> <relativeLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:padding="2dp"> <TextVIEw androID:ID="@+ID/itemTitle" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParentleft="true" androID:layout_alignParentStart="true" androID:layout_centerVertical="true" androID:layout_gravity="center_vertical" androID:layout_toleftOf="@+ID/btnMore" androID:text="Sample Title" androID:textcolor="@androID:color/black" androID:textSize="18sp" /> <button androID:ID="@+ID/btnMore" androID:layout_wIDth="wrap_content" androID:layout_height="42dp" androID:layout_alignParentEnd="true" androID:layout_alignParentRight="true" androID:layout_centerVertical="true" androID:theme="@style/Mybutton" androID:text="more" androID:textcolor="#FFF" /> </relativeLayout> <androID.support.v7.Widget.RecyclerVIEw androID:ID="@+ID/recycler_vIEw_List" androID:layout_wIDth="match_parent" androID:layout_height="160dp" androID:layout_gravity="center_vertical" androID:orIEntation="horizontal" /></linearLayout>
第四步
现在,为了制作水平的RecyclerVIEw,我们需要为每一行创建一个布局和Adapter类.
List_single_card.xml
<?xml version="1.0" enCoding="utf-8"?><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="wrap_content" androID:orIEntation="horizontal" app:cardCornerRadius="5dp" app:cardUseCompatpadding="true" > <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:padding="0dp" androID:background="?androID:selectableItemBackground" androID:orIEntation="vertical"> <ImageVIEw androID:ID="@+ID/itemImage" androID:layout_wIDth="100dp" androID:layout_height="100dp" androID:layout_gravity="center_horizontal" androID:scaleType="fitCenter" androID:src="@drawable/androID" /> <TextVIEw androID:ID="@+ID/tvTitle" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_below="@ID/itemImage" androID:gravity="center" androID:padding="5dp" androID:text="Sample Title" androID:textcolor="@androID:color/black" androID:textSize="18sp" /> </linearLayout></androID.support.v7.Widget.CardVIEw>
>最后,适用于Horizontal RecyclerVIEw的适配器类
SectionListDataAdapter.java
package-name-here.adapters;import androID.content.Context;import androID.support.v7.Widget.RecyclerVIEw;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.ImageVIEw;import androID.Widget.TextVIEw;import androID.Widget.Toast;import com.pratap.gplaystore.R;import com.pratap.gplaystore.models.SingleItemmodel;import java.util.ArrayList;public class SectionListDataAdapter extends RecyclerVIEw.Adapter<SectionListDataAdapter.SingleItemRowHolder> { private ArrayList<SingleItemmodel> itemsList; private Context mContext; public SectionListDataAdapter(Context context, ArrayList<SingleItemmodel> itemsList) { this.itemsList = itemsList; this.mContext = context; } @OverrIDe public SingleItemRowHolder onCreateVIEwHolder(VIEwGroup vIEwGroup, int i) { VIEw v = LayoutInflater.from(vIEwGroup.getContext()).inflate(R.layout.List_single_card, null); SingleItemRowHolder mh = new SingleItemRowHolder(v); return mh; } @OverrIDe public voID onBindVIEwHolder(SingleItemRowHolder holder, int i) { SingleItemmodel singleItem = itemsList.get(i); holder.tvTitle.setText(singleItem.getname()); /* GlIDe.with(mContext) .load(FeedItem.getimageURL()) .diskCacheStrategy(diskCacheStrategy.ALL) .centerCrop() .error(R.drawable.bg) .into(FeedListRowHolder.thumbVIEw);*/ } @OverrIDe public int getItemCount() { return (null != itemsList ? itemsList.size() : 0); } public class SingleItemRowHolder extends RecyclerVIEw.VIEwHolder { protected TextVIEw tvTitle; protected ImageVIEw itemImage; public SingleItemRowHolder(VIEw vIEw) { super(vIEw); this.tvTitle = (TextVIEw) vIEw.findVIEwByID(R.ID.tvTitle); this.itemImage = (ImageVIEw) vIEw.findVIEwByID(R.ID.itemImage); vIEw.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { Toast.makeText(v.getContext(), tvTitle.getText(), Toast.LENGTH_SHORT).show(); } }); } }}
总结 以上是内存溢出为你收集整理的android – 水平滚动的水平回收视图[复制]全部内容,希望文章能够帮你解决android – 水平滚动的水平回收视图[复制]所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)