显示两列ListFragment

时间:2014-03-28 05:12:52

标签: android

我使用Listfragmen,其中适配器输出图像。

我需要在两列中显示图像。

我尝试放入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?

1 个答案:

答案 0 :(得分:1)

请使用GridView显示两列,它很容易像列表视图一样处理。

检查我的xml下面是否创建了两个列gridview并设置了像listview

这样的适配器
   <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>