ListView元素垂直显示,我希望它们水平显示。

时间:2015-04-17 17:49:27

标签: android android-fragments android-listview

我有一个ListView,其中元素垂直显示,我希望它们水平显示 我该怎么做?

来自MyFragment.java的

private static final String[] MyArray = new String[] { "Id", "Type", "Name"};

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {

      final Activity thisActivity = getActivity();
      final ListView listview = (ListView)thisActivity.findViewById(R.id.my_listview);

      final List<String> list = Arrays.asList(MyArray);

      final MyListAdapter adapter = new MyListAdapter(this.getActivity(), R.layout.row_layout, list);
      listview.setAdapter(adapter);
 }

fragment.xml之

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="#000000" >

<ListView
    android:id="@+id/my_listview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" /> 

</RelativeLayout>

row_layout.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="horizontal">

    <TextView
        android:id="@+id/id"
        android:textColor="#FFFFFF"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp" >
    </TextView>

    <TextView
        android:id="@+id/type"
        android:textColor="#FFFFFF"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp" >
    </TextView>

    <TextView
        android:id="@+id/name"
        android:textColor="#FFFFFF"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp" >
    </TextView>

</LinearLayout> 

MyListAdapter.java

public class MyListAdapter extends ArrayAdapter<String> {

    public final static int ID = 0;
    public static final int TYPE =  1;
    public static final int NAME = 2;

    private final Context context;
    private final List<String> stringList;

    public MyListAdapter(Context context,  int textViewResourceId, List<String> values) {
        super(context, textViewResourceId, values);
        this.context = context;
        this.stringList = values;
}

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.row_layout, parent, false);

        // need to get correct textView based on position
        TextView textView = null;
        switch (position) {
            case ID:
                textView = (TextView) rowView.findViewById(R.id.id);
                break;
            case TYPE:
                textView = (TextView) rowView.findViewById(R.id.type);
                break;
            case NAME:
                textView = (TextView) rowView.findViewById(R.id.name);
                break;
        }

        textView.setText(stringList.get(position));

        return rowView;
    }

    @Override
    public int getCount() {
        return stringList.size();
    }

    @Override
    public String getItem(int position) {
        return stringList.get(position);
    }

}

2 个答案:

答案 0 :(得分:2)

使用Android API中的ListView水平显示ListView个项目是不可能的。但是有些库可以实现(只需搜索&#34; Horizo​​ntal ListView&#34;)。但您可以使用新的RecyclerView

执行此操作

答案 1 :(得分:2)

您尝试使用此水平列表视图,我在我的所有项目中使用此项目,这是一个非常好的项目!

Horizontal ListView