如何将Listview添加到Listview Android Studio中



我想将列表视图添加到我的列表视图中。这就像一本简单的旅行书。用户将单击其中一个国家/地区并进入另一个列表视图。我可以用图像轻松解释

这是我的Listview:http://prntscr.com/mhwic4

我想为所有国家添加一个城市。比如罗马,威尼斯,佛罗伦萨进入意大利,悉尼进入澳大利亚,里约热内卢进入巴西。

有我的主要活动代码:

public class MainActivity extends AppCompatActivity {
ListView listView;
String[] nameArray = {"İtalya","Almanya","Amerika","Avustralya","Brezilya","Çin","Dubai","Fransa","İsviçre","Hollanda","İspanya","Kanada","Macaristan","Rusya",
"Sırbistan","Yunanistan"};
String[] infoArray = {
        "Buon divertimento!",
        "Viel spaß",
        "Have fun!",
        "Have fun!",
        "Divirta-se!",
        "玩得开心!",
        "استمتع!",
        "Amusez vous bien!",
        "Viel spaß",
        "Veel plezier!",
        "Diviertete!",
        "Have fun!",
        "Jó szórakozást!",
        "Веселись!",
        "Забавите се!",
        "Διασκεδάστε!"

};
Integer[] imageArray = {R.drawable.italy,
        R.drawable.germany,
        R.drawable.usa,
        R.drawable.aus,
        R.drawable.brazil,
        R.drawable.china,
        R.drawable.dubai,
        R.drawable.france,
        R.drawable.swit,
        R.drawable.netherlands,
        R.drawable.spain,
        R.drawable.canada,
        R.drawable.macaristan,
        R.drawable.russia,
        R.drawable.serbia,
        R.drawable.greece
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

        CustomListAdapter whatever = new CustomListAdapter(this, nameArray, infoArray, imageArray);
        listView = (ListView) findViewById(R.id.listviewID);
        listView.setAdapter(whatever);
    }
}

这是适配器代码:

public class CustomListAdapter extends ArrayAdapter {

public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater=context.getLayoutInflater();
    View rowView=inflater.inflate(R.layout.listview_row, null,true);
    //this code gets references to objects in the listview_row.xml file
    TextView nameTextField = (TextView) rowView.findViewById(R.id.nameTextViewID);
    TextView infoTextField = (TextView) rowView.findViewById(R.id.infoTextViewID);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView1ID);
    //this code sets the values of the objects to values from the arrays
    nameTextField.setText(nameArray[position]);
    infoTextField.setText(infoArray[position]);
    imageView.setImageResource(imageIDarray[position]);
    return rowView;
}
//to reference the Activity
private final Activity context;
//to store the animal images
private final Integer[] imageIDarray;
//to store the list of countries
private final String[] nameArray;
//to store the list of countries
private final String[] infoArray;
public CustomListAdapter(Activity context, String[] nameArrayParam, String[] infoArrayParam, Integer[] imageIDArrayParam) {
    super(context, R.layout.listview_row, nameArrayParam);
    this.context=context;
    this.imageIDarray = imageIDArrayParam;
    this.nameArray = nameArrayParam;
    this.infoArray = infoArrayParam;
  }
 }

我不完全确定你在问什么,但你可能正在寻找一个可扩展列表视图。这样,您就可以在同一视图中列出每个国家/地区的城市。用户点击一个国家/地区以展开它并显示其城市。

使用大纲/详细信息流可能更容易,并且在不同设备上更有效,其中详细信息片段显示主列表中所选国家/地区的城市列表。请参阅跨手机和平板电脑实现大纲/详细信息流。

通过实现可扩展布局来尝试此操作,以便用户可以轻松访问另一个列表。

implementation 'com.github.aakira:expandable-layout:1.6.0@aar'

在你的.xml-

   <com.github.aakira.expandablelayout.ExpandableRelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/expandableLinearLayout2"
        android:layout_marginTop="5dp"
        app:ael_expanded="false"
        app:ael_duration="600"
        app:layout_constraintTop_toTopOf="parent"
        app:ael_interpolator="accelerate"
        app:ael_orientation="vertical"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent">
        <<<<<YOUR LIST VIEW AND OTHER ELEMENTS>>>>
</com.github.aakira.expandablelayout.ExpandableRelativeLayout>

使用-

expandableRelativeLayout = v.findViewById(R.id.expandableLinearLayout2)
        button.setOnClickListener {
            expandableRelativeLayout!!.expand()
        }
有一个

本机连接可扩展回收器视图使用它而不是ListView或其他库

这是一个例子: https://acadgild.com/blog/expandable-recyclerview-in-android-with-examples

最新更新