如何在ListView中的每一行中显示多个TextView?

时间:2013-12-24 05:38:47

标签: android listview android-custom-view custom-adapter

我正在创建一个帮助页面,其中包含一系列问题和答案。这些问题和答案有不同的风格。这是xml文件,它描述了问题&的布局。答案集

<?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="wrap_content"
              android:orientation="vertical"
              android:layout_gravity="center">
    <TextView
            android:text="@string/Help_first_question"
            android:id="@+id/text1"
            android:padding="5dip"
            android:background="#e0f3ff"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
    <LinearLayout
            android:id="@+id/panel1"
            android:visibility="gone"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
        <TextView
                android:layout_margin="2dip"
                android:text="@string/Help_first_answer"
                android:padding="5dip"
                android:background="#FFFFFF"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

我想在listView内显示多个问题和答案,每行包含一组问题和答案。我的listview看起来像是:

    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/listview"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" >  
    </ListView>

所以它看起来像:

first row  :  Q
              A
second row :  Q
              A
third row  :  Q
              A 

实现这一目标的最佳方法是什么?

2 个答案:

答案 0 :(得分:1)

创建自定义适配器并使用以下布局来实现目标

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

</LinearLayout>

答案 1 :(得分:1)

您需要实现一个实现所有抽象方法的自定义ListAdapter

让我们创建一个QuestionAndAnswerListAdapter,您可以通过在ListView中设置onCreate来创建public void onCreate(Bundle savedInstanceState) { super(savedInstanceState); setContentView(R.layout.listview); QuestionsAndAnswersListAdapter adapter = new QuestionsAndAnswersListAdapter(data); ListView listView = (ListView) findViewById(R.id.listview); listView.setAdapter(adapter); }

public QuestionsAndAnswersListAdapter implements ListAdapter {
    private QuestionAndAnswer[] data;

    public QuestionsAndAnswersListAdapter(QuestionAndAnswer[] data) {
        this.data = data;
    }

    public View getView(int position, View view, ViewGroup parent) {
        if(view == null) {
            //Only creates new view when recycling isn't possible
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.question_and_answer_list_item, null);
        }

        QuestionAndAnswer thisQA = data[position];

        TextView questionView = (TextView) view.findViewById(R.id.text1);
        questionView.setText(thisQA.question);

        TextView answerView = (TextView) view.findViewById(R.id.answer);
        answerView.setText(thisQA.answer);

        return view;
    }

    // ...
}

适配器本身看起来像这样:

getView

ListAdapter确实是正确的核心方法。您需要实现的其他方法才能实现<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_centerInParent="true" android:foregroundGravity="center" android:gravity="center" android:orientation="horizontal"> <View android:layout_width="15dp" android:layout_height="1dp" android:background="@android:color/black" /> <View android:layout_width="5dp" android:layout_height="1dp" android:background="@android:color/transparent" /> <View android:layout_width="2dp" android:layout_height="2dp" android:background="@android:color/black" /> <View android:layout_width="5dp" android:layout_height="1dp" android:background="@android:color/transparent" /> <View android:layout_width="15dp" android:layout_height="1dp" android:background="@android:color/black" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_centerInParent="true" android:foregroundGravity="center" android:gravity="center" android:orientation="vertical"> <View android:layout_width="1dp" android:layout_height="15dp" android:background="@android:color/black" /> <View android:layout_width="1dp" android:layout_height="11dp" android:background="@android:color/transparent" /> <View android:layout_width="1dp" android:layout_height="15dp" android:background="@android:color/black" /> </LinearLayout> 界面非常简单。检查reference以确切了解它们是什么。