我做了一个自定义的“水平列表”视图,它工作正常.然后,我在ListVIEw控件中实现了相同的水平列表.但是,当我运行相同的命令时,得到的只是空白输出.该列表似乎没有增加.
在调试时,我发现添加到列表视图的适配器包含项(水平列表).
因此,问题显然不在于项目未正确填充,其视图未正确呈现.
这是垂直列表视图的代码:
XML:
<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:orIEntation="vertical" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <com.example.twowayListvIEw.horizontallistVIEw androID:ID="@+ID/lstHor" androID:layout_height="100dp" androID:layout_wIDth="fill_parent"> </com.example.twowayListvIEw.horizontallistVIEw></linearLayout>
这是水平列表中单个项目的XML
<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="100dp"> <ImageVIEw androID:ID="@+ID/imgListItem" androID:layout_height="85dp" androID:layout_wIDth="85dp" .. /> ...</relativeLayout>
让我知道您是否需要Java代码.谢谢.
解决方法:
发布AndroID L开发者预览版后,Google提供了一个名为RecyclerVIEw的新视图,该视图可以替代ListVIEw和GrIDVIEw,这样可以更轻松地创建水平列表,因此我在此处更新了答案.
目前,我们不再需要使用任何库,RecyclerVIEw就足够了.这是我通过RecyclerVIEw列出水平代码的代码(我不打算解释RecyclerVIEw的详细用法):
MainActivity.java
public class MainActivity extends AppCompatActivity { List<List<String>> mDataList; private RecyclerVIEw mVerticalList; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); prepareData(); initListVIEw(); } private voID prepareData() { mDataList = new ArrayList<>(); int vItemCount = 25; int hItemCount = 20; for (int i = 0; i < vItemCount; i++) { List<String> hList = new ArrayList<>(); for (int j = 0; j < hItemCount; j++) { hList.add("Item." + j); } mDataList.add(hList); } } private voID initListVIEw() { mVerticalList = (RecyclerVIEw) findVIEwByID(R.ID.vertical_List); mVerticalList.setLayoutManager(new linearlayoutmanager(this, linearlayoutmanager.VERTICAL, false)); VerticalAdapter verticalAdapter = new VerticalAdapter(); verticalAdapter.setData(mDataList); mVerticalList.setAdapter(verticalAdapter); } private static class VerticalAdapter extends RecyclerVIEw.Adapter<RecyclerVIEw.VIEwHolder> { private List<List<String>> mDataList; public VerticalAdapter() { } public voID setData(List<List<String>> data) { mDataList = data; notifyDataSetChanged(); } private class horizontallistVIEwHolder extends RecyclerVIEw.VIEwHolder { private TextVIEw Title; private RecyclerVIEw horizontallist; private HorizontalAdapter horizontalAdapter; public horizontallistVIEwHolder(VIEw itemVIEw) { super(itemVIEw); Context context = itemVIEw.getContext(); Title = (TextVIEw) itemVIEw.findVIEwByID(R.ID.item_Title); horizontallist = (RecyclerVIEw) itemVIEw.findVIEwByID(R.ID.item_horizontal_List); horizontallist.setLayoutManager(new linearlayoutmanager(context, linearlayoutmanager.HORIZONTAL, false)); horizontalAdapter = new HorizontalAdapter(); horizontallist.setAdapter(horizontalAdapter); } } @OverrIDe public RecyclerVIEw.VIEwHolder onCreateVIEwHolder(VIEwGroup parent, int vIEwType) { Context context = parent.getContext(); VIEw itemVIEw = LayoutInflater.from(context).inflate(R.layout.vertical_List_item, parent, false); horizontallistVIEwHolder holder = new horizontallistVIEwHolder(itemVIEw); return holder; } @OverrIDe public voID onBindVIEwHolder(RecyclerVIEw.VIEwHolder rawHolder, int position) { horizontallistVIEwHolder holder = (horizontallistVIEwHolder) rawHolder; holder.Title.setText("Horizontal List No." + position); holder.horizontalAdapter.setData(mDataList.get(position)); holder.horizontalAdapter.setRowIndex(position); } @OverrIDe public int getItemCount() { return mDataList.size(); } } private static class HorizontalAdapter extends RecyclerVIEw.Adapter<RecyclerVIEw.VIEwHolder> { private List<String> mDataList; private int mRowIndex = -1; private int[] mcolors = new int[]{color.RED, color.BLUE, color.GREEN, color.magenta, color.DKGRAY}; public HorizontalAdapter() { } public voID setData(List<String> data) { if (mDataList != data) { mDataList = data; notifyDataSetChanged(); } } public voID setRowIndex(int index) { mRowIndex = index; } private class ItemVIEwHolder extends RecyclerVIEw.VIEwHolder { private TextVIEw text; public ItemVIEwHolder(VIEw itemVIEw) { super(itemVIEw); text = (TextVIEw) itemVIEw.findVIEwByID(R.ID.item_text); itemVIEw.setonClickListener(mItemClickListener); } } @OverrIDe public RecyclerVIEw.VIEwHolder onCreateVIEwHolder(VIEwGroup parent, int vIEwType) { Context context = parent.getContext(); VIEw itemVIEw = LayoutInflater.from(context).inflate(R.layout.horizontal_List_item, parent, false); ItemVIEwHolder holder = new ItemVIEwHolder(itemVIEw); return holder; } @OverrIDe public voID onBindVIEwHolder(RecyclerVIEw.VIEwHolder rawHolder, int position) { ItemVIEwHolder holder = (ItemVIEwHolder) rawHolder; holder.text.setText(mDataList.get(position)); holder.itemVIEw.setBackgroundcolor(mcolors[position % mcolors.length]); holder.itemVIEw.setTag(position); } @OverrIDe public int getItemCount() { return mDataList.size(); } private VIEw.OnClickListener mItemClickListener = new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { int columnIndex = (int) v.getTag(); int rowIndex = mRowIndex; String text = String.format("rowIndex:%d ,columnIndex:%d", rowIndex, columnIndex); showToast(v.getContext(), text); Log.d("test", text); } }; } private static Toast sToast; public static voID showToast(Context context, String text) { if (sToast != null) { sToast.cancel(); } sToast = Toast.makeText(context, text, Toast.LENGTH_LONG); sToast.show(); }}
activity_main.xml中
<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <androID.support.v7.Widget.RecyclerVIEw androID:ID="@+ID/vertical_List" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"/></relativeLayout>
horizontal_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:paddingleft="10dp" androID:paddingRight="10dp" androID:orIEntation="vertical"> <TextVIEw androID:ID="@+ID/item_text" androID:textcolor="@androID:color/white" androID:layout_wIDth="match_parent" androID:layout_height="50dp" androID:gravity="center_vertical"/></linearLayout>
vertical_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:orIEntation="vertical"> <TextVIEw androID:ID="@+ID/item_Title" androID:background="@androID:color/darker_gray" androID:layout_wIDth="match_parent" androID:layout_height="48dp" androID:gravity="center_vertical"/> <androID.support.v7.Widget.RecyclerVIEw androID:ID="@+ID/item_horizontal_List" androID:layout_wIDth="match_parent" androID:layout_height="50dp"/></linearLayout>
于2015-05-20更新
旧答案:
我已经使用TwoWayView像您一样在垂直列表视图中显示水平列表.这是我的垂直和水平ListvIEw适配器代码.它对我来说很好用,所有视图都是可重用的,所有行都单独滚动.希望对您有所帮助.
public class RecommendAppAdapter extends BaseAdapter implements OnItemClickListener {public static final String TAG = "RecommendAppAdapter";//Vertical List dataprivate Map<String, List<RecommendApp>> mMapData = new TreeMap<String, List<RecommendApp>>();private OnItemClickListener mOnItemClickListener = null;//Vertical List adapterpublic RecommendAppAdapter(Context context) {}@OverrIDepublic boolean isEmpty() { return mMapData == null || mMapData.isEmpty();}@OverrIDepublic int getCount() { if (!isEmpty()) { return mMapData.size(); } else { return 0; }}//Get horiZental List data@OverrIDepublic List<RecommendApp> getItem(int position) { if (!isEmpty() && isAvaliablePostion(position)) { Iterator<Entry<String, List<RecommendApp>>> entrIEs = mMapData .entrySet().iterator(); int i = 0; Entry<String, List<RecommendApp>> entry = null; while (entrIEs.hasNext()) { entry = entrIEs.next(); if (i == position) { return entry.getValue(); } i++; } } return null;}@OverrIDepublic long getItemID(int position) { return position;}@OverrIDepublic VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) { VIEwHolder holder = null; ItemAdapter innerlistadapter = null; if (convertVIEw == null) { holder = new VIEwHolder(); convertVIEw = LayoutInflater.from(parent.getContext()).inflate( R.layout.item_recommend_app, null); //Some vIEws in vertical List holder.type = (TextVIEw) convertVIEw .findVIEwByID(R.ID.item_rec_app_type_name); //Get horiZental List vIEw holder.hListVIEw = (TwoWayVIEw) convertVIEw .findVIEwByID(R.ID.item_rec_app_List); //Bind adapter on horiZental List innerlistadapter = new ItemAdapter(); holder.hListVIEw.setAdapter(innerlistadapter); //Bind item click Listener on horiZental List holder.hListVIEw.setonItemClickListener(this); convertVIEw.setTag(holder); } else { holder = (VIEwHolder) convertVIEw.getTag(); innerlistadapter = (ItemAdapter) holder.hListVIEw.getAdapter(); } //Get horiZental List data List<RecommendApp> itemList = getItem(position); holder.type.setText(itemList.get(0).getTypename()); //Deliver horiZental List adapter data innerlistadapter.setData(itemList); return convertVIEw;}@OverrIDepublic voID onItemClick(AdapterVIEw<?> parent, VIEw vIEw, int position, long ID) { switch (parent.getID()) { case R.ID.item_rec_app_List: if (mOnItemClickListener != null) { mOnItemClickListener.onItemClick(parent, vIEw, position, ID); } break; }}public voID setonItemClickListener(OnItemClickListener Listener) { mOnItemClickListener = Listener;}public voID setData(Map<String, List<RecommendApp>> data) { mMapData.clear(); if (data != null && !data.isEmpty()) { mMapData.putAll(data); } notifyDataSetChanged();}private boolean isAvaliablePostion(int position) { if (position >= 0 && position < getCount()) { return true; } else { return false; }}private class VIEwHolder { public TextVIEw type; public TwoWayVIEw hListVIEw;}//Horizontal List adapter//All work are just like the normal use of ListVIEw private class ItemAdapter extends BaseAdapter { //Horizontal List data private List<RecommendApp> mInnerData = null; private FinalBitmap mFinalBitmap = FinalBitmap.create(MyApp .getInstance()); @OverrIDe public boolean isEmpty() { return mInnerData == null || mInnerData.isEmpty(); } @OverrIDe public int getCount() { if (!isEmpty()) { return mInnerData.size(); } return 0; } @OverrIDe public RecommendApp getItem(int position) { if (position >= 0 && position < getCount()) { return mInnerData.get(position); } return null; } @OverrIDe public long getItemID(int position) { return position; } @OverrIDe public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) { VIEwHolderInner holder = null; if (convertVIEw == null) { holder = new VIEwHolderInner(); Context context = parent.getContext(); convertVIEw = LayoutInflater.from(context).inflate( R.layout.item_recommend_app_inner, null); holder.name = (TextVIEw) convertVIEw .findVIEwByID(R.ID.item_rec_app_inner_name); holder.icon = (ImageVIEw) convertVIEw .findVIEwByID(R.ID.item_rec_app_inner_icon); convertVIEw.setTag(holder); } else { holder = (VIEwHolderInner) convertVIEw.getTag(); } RecommendApp item = ItemAdapter.this.getItem(position); holder.name.setText(item.getAppname()); mFinalBitmap.display(holder.icon, item.getIcon()); return convertVIEw; } public voID setData(List<RecommendApp> data) { mInnerData = data; notifyDataSetChanged(); } private class VIEwHolderInner { public TextVIEw name; public ImageVIEw icon; }}
}
编辑1
RecommendApp是一个POJO,其中包含在水平列表中显示的项目数据.
item_recommend_app.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/item_rec_app_type_name" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:background="@color/item_rec_app_type_name_bg" androID:gravity="left|center_vertical" androID:paddingBottom="@dimen/item_rec_app_type_name_paddingBottom" androID:paddingleft="@dimen/item_rec_app_type_name_paddingleft" androID:paddingtop="@dimen/item_rec_app_type_name_paddingtop" androID:textAppearance="?androID:attr/textAppearanceMedium" androID:textIsSelectable="false" /><com.phonetools.appmanager.Widget.TwoWayVIEw androID:ID="@+ID/item_rec_app_List" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:orIEntation="horizontal" />
item_recommend_app_inner.xml:
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:layout_wIDth="wrap_content"androID:layout_height="wrap_content"androID:orIEntation="vertical"androID:padding="@dimen/item_rec_app_inner_padding" ><ImageVIEw androID:ID="@+ID/item_rec_app_inner_icon" androID:layout_wIDth="@dimen/item_rec_app_inner_icon_wIDth" androID:layout_height="@dimen/item_rec_app_inner_icon_height" androID:layout_gravity="center" androID:contentDescription="@string/image_desc" androID:scaleType="fitCenter" /><TextVIEw androID:ID="@+ID/item_rec_app_inner_name" androID:layout_wIDth="@dimen/item_rec_app_inner_icon_wIDth" androID:layout_height="wrap_content" androID:layout_gravity="center" androID:ellipsize="end" androID:gravity="center" androID:singleline="true" androID:textAppearance="?androID:attr/textAppearanceSmall" androID:textIsSelectable="false" />
RecommendApp.java:
public class RecommendApp {public RecommendApp() {}private String packagename;private String appname;private String versionname;private float size;private String icon;private int typeID;private String typename;private String installlink;private String description;private float ratingscore;private long installedSum;
}
总结以上是内存溢出为你收集整理的在Android的垂直列表内实现horizontalList全部内容,希望文章能够帮你解决在Android的垂直列表内实现horizontalList所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)