2017-10-11 66 views
-1

我有一個ListViewAndroid的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更改爲android.support.v4.view.ViewPager,但同一個適配器無法正常工作。 有沒有辦法改變ListView作爲一個TabControl作爲一個水平的?

我是新Android中的編程所以請溫柔:)

+0

你有沒有試過用'RecyclerView'呢? –

+0

我已經看過它,但我並沒有完全理解它是如何工作的,這就是爲什麼我試過ViewPager – Dana

+1

儘管存在困難,但您仍需要學習新的東西。 –

回答

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