Java 如何将Listview添加到Listview Android Studio
我想将Listview添加到我的Listview中。这就像一本简单的旅游书。用户要单击其中一个国家并进入另一个列表视图。我能用图像很容易地解释 这是我的Java 如何将Listview添加到Listview Android Studio,java,android,android-studio,Java,Android,Android Studio,我想将Listview添加到我的Listview中。这就像一本简单的旅游书。用户要单击其中一个国家并进入另一个列表视图。我能用图像很容易地解释 这是我的列表视图: 我想为所有国家添加一个城市。像罗马,威尼斯,弗洛伦斯到意大利,悉尼到澳大利亚,里约热内卢到巴西等等 以下是我的主要活动代码: public class MainActivity extends AppCompatActivity { ListView listView; String[] nameArray = {"İtalya"
列表视图
:
我想为所有国家添加一个城市。像罗马,威尼斯,弗洛伦斯到意大利,悉尼到澳大利亚,里约热内卢到巴西等等
以下是我的主要活动代码:
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;
}
}
通过实现一个可扩展的布局来尝试这一点,这样用户就可以很容易地访问另一个列表 在您的.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>
通过实现一个可扩展的布局来尝试这一点,这样用户就可以很容易地访问另一个列表 在您的.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>
我不完全确定你问的是什么,但你可能正在寻找一份工作。这样可以在同一视图中列出每个国家的城市。用户点击一个国家来扩展它并展示它的城市
对于您来说,使用主/详细流程可能更容易,在不同的设备上也更有效,其中详细信息片段显示主列表中所选国家/地区的城市列表。请参阅。我不完全确定您在问什么,但您可能正在寻找一份工作。这样可以在同一视图中列出每个国家的城市。用户点击一个国家来扩展它并展示它的城市
对于您来说,使用主/详细流程可能更容易,在不同的设备上也更有效,其中详细信息片段显示主列表中所选国家/地区的城市列表。请参阅。有一个本机组件expandableRecyclerview请使用它,而不是ListView或其他LIB
这是一个示例:有一个本机组件expandableRecyclerview使用它而不是ListView或其他LIB
这是一个例子:您的问题不太清楚,但您可能正在寻找可扩展的ListView。或者可能是一个主/细节流,其中细节片段是所选国家/地区的城市列表。不太清楚您在问什么,但您可能正在寻找可扩展的ListView。或者可能是一个主/细节流,其中细节片段是所选国家/地区的城市列表。是的,这是推荐的方式。是的,这是推荐的方式。
expandableRelativeLayout = v.findViewById(R.id.expandableLinearLayout2)
button.setOnClickListener {
expandableRelativeLayout!!.expand()
}