2014-03-28 81 views
1

我使用Listfragmen,其中您的適配器輸出圖像。顯示兩列ListFragment

我需要在兩列顯示圖像。

我嘗試把listView片段,但數據不顯示。

我需要有兩列顯示圖像。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical"> 
    <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
     <ListView 
       android:id="@id/android:list" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" android:layout_weight="1"> 
     </ListView> 
     <ListView android:id="@id/android:list" android:layout_width="match_parent" android:layout_height="wrap_content" 
        android:layout_weight="1"/> 
    </LinearLayout> 
</LinearLayout> 

適配器代碼

public class MyAdapter extends ArrayAdapter<Map<String, Object>> { 
    private final Activity context; 
    private final List<Map<String, Object>> datas; 

    public MyAdapter(Activity context, List<Map<String, Object>> datas) { 
     super(context, R.layout.items, datas); 
     this.context = context; 
     this.datas = datas; 
    } 

    class ViewHolder { 
     public TextView tvHeader; 
     public ImageView ivImage; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     Map<String, Object> itemData = datas.get(position); 
     Bitmap bitmap = (Bitmap) itemData.get("img"); 
     View rowView = convertView; 
     if (rowView == null) { 
      LayoutInflater inflater = context.getLayoutInflater(); 
      rowView = inflater.inflate(R.layout.items, null, true); 
      holder = new ViewHolder(); 
      holder.tvHeader = (TextView) rowView.findViewById(R.id.tvText); 
      holder.ivImage = (ImageView) rowView.findViewById(R.id.ivImg); 
      rowView.setTag(holder); 
     } else { 
      holder = (ViewHolder) rowView.getTag(); 
     } 

     holder.tvHeader.setText((String) itemData.get(MyActivity.ATTRIBUTE_NAME_TEXT)); 
     holder.ivImage.setImageBitmap(bitmap); 
     return rowView; 
    } 
} 

片段代碼

public class PlanetFragment extends ListFragment{ 
     Bundle bundle; 
     public int itpos ; 
     public String link; 
     MyAdapter sAdapter; 
     ArrayList<Map<String, Object>> data1; 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    data1 = new ArrayList<Map<String, Object>>(); 
    bundle = getArguments(); 
    itpos = bundle.getInt("position"); 
    link = bundle.getString("link"); 
    context = getActivity(); 
    NewThread t = new NewThread(); 
    t.setOnParseResult(new IOnParseResult() { 
     @Override 
     public void onDone(ArrayList<Map<String, Object>> data) { 
      data1 = data; 
      sAdapter = new MyAdapter(getActivity(), data1); 
      sAdapter.notifyDataSetChanged(); 
      setListAdapter(sAdapter); 
     } 

     @Override 
     public void onVideoLinkGet(String str) {} 
    }); 
    t.execute(link,"link"); 

} 
     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      return inflater.inflate(R.layout.videolist, null); 
     } 

     public void onListItemClick(ListView l, View v, int position, long id) { 
      super.onListItemClick(l, v, position, id); 
      Toast.makeText(getActivity(),data1.get(position).get("img").toString(), Toast.LENGTH_SHORT).show(); 
     } 
    } 

如何顯示兩列ListFragment?

+0

爲什麼你有2名列表視圖,以及哪裏是listfragment代碼 – Raghunandan

+0

我只是不知道該怎麼做,這樣做( (((( – IP696

+0

使用GridView爲您的要求 –

回答

1

請使用GridView顯示兩列,這很容易處理像列表視圖一樣。

檢查我下面的XML創建兩個列GridView和設置適配器像列表視圖

<GridView 
     android:id="@+id/event_list" 
     android:layout_width="fill_parent" 
     android:layout_height="200dp" 
     android:horizontalSpacing="5dp" 
     android:numColumns="2"// check this line for making two column 
     android:padding="5dp" 
     android:scrollbars="none" 
     android:scrollingCache="false" 
     android:verticalSpacing="5dp" > 
    </GridView> 
+0

如何設置列表到GridView? – IP696

+0

以及你是創建適配器然後將適配器設置爲gridview –