簡體   English   中英

如何將Listview添加到Listview Android Studio中

[英]How can i add Listview into Listview Android Studio

我想將Listview添加到我的listview中。 這就像一本簡單的旅行書。 用戶將要單擊其中一個國家並進入另一個列表視圖。 我可以用圖片輕松解釋

有我的Listviewhttp : //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;
  }
 }

我不確定您要問什么,但您可能正在尋找ExpandableListView 這樣一來,您就可以在同一視圖中列出每個國家/地區的城市。 用戶點擊一個國家即可擴展它並顯示其城市。

使用“主/明細”流程可能會更方便,並且在其他設備上更有效,其中“明細”片段將顯示“主”列表中所選國家/地區的城市列表。 請參閱在手機和平​​板電腦上實現主/細節流

通過實現可擴展布局來嘗試此操作,以便用戶可以輕松訪問另一個列表。

 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()
        }

有一種天然的conponent expandableRecyclerview用它代替ListView控件或其他庫

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM