Android ListView到水平标签

时间:2017-10-11 10:10:11

标签: android listview

我有一个ListView

<ListView
    android:id="@+id/group"
    android:layout_width="264dp"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"/>

ListView gvGroup = ((ListView)findViewById(R.id.group));
    gvGroup.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
            details=fManager.getPageItems(fMenu,(int) id);
            ((BaseAdapter)gvDetail.getAdapter()).notifyDataSetChanged();
        }
    });
    gvGroup.setAdapter(new GroupListAdapter());
    gvGroup.postDelayed(new Runnable() {
        public void run() {
            gvGroup.setItemChecked(0, true);
        }
    }, 50);

并且适配器是

private final class GroupListAdapter extends BaseAdapter{
    public int getCount() {return groups!=null?groups.size():0;}
    public Object getItem(int position) {return groups.get(position);}
    public long getItemId(int position) {return groups.get(position).Id;}
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView tv=(TextView) convertView;
        if(tv==null){
            float size_font_dp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 28, getResources().getDisplayMetrics());
            float width_dp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 72, getResources().getDisplayMetrics());
            tv = new TextView(MyClass.this);
            tv.setBackgroundResource(R.drawable.list_selector);
            tv.setTextSize(size_font_dp);tv.setTypeface(Typeface.DEFAULT_BOLD);
            tv.setSingleLine();tv.setGravity(Gravity.CENTER_VERTICAL);
            AbsListView.LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,(int)(width_dp));              
            tv.setLayoutParams(lp);
        }
        tv.setText(((FPage)getItem(position)).Name);
        return tv;
    }
}

我的问题是我需要将此ListView转换为可以滚动的水平ListView。我试图将我的ListView更改为android.support.v4.view.ViewPager,但同一个适配器无法正常工作。 有没有办法将ListView作为TabControl更改为水平的?

我是Android编程的新手,所以请保持温和:)

1 个答案:

答案 0 :(得分:1)

您可以使用RecyclerView获得所需的结果。您可以从以下链接中找到RecyclerView的示例: http://www.androhub.com/android-staggered-and-horizontal-recyclerview/

要使用RecyclerView创建水平列表,您可以执行以下操作:

 final LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);

    final RecyclerView myList = (RecyclerView) findViewById(R.id.recycler_view);
    myList.setLayoutManager(layoutManager);