我是Android新手。我在android中开发了一个应用程序,最低API级别为15,最高API级别为23。
我想要like this,另一件事是子导航中的多选项。从父导航视图中选择任何项目时,将打开子导航视图,如图像所示。我搜索谷歌但没有得到答案。我看到this和其他如此多的堆栈溢出答案但没有得到。如果有人帮助的话,那就非常感谢。谢谢。
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/lvList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"/>
</LinearLayout>
这是我的javafile
public class HomePageActivity extends AppCompatActivity
{
ListView lvList;
ArrayList<String> arrayList;
NavigationViewAdapter adapter;
ArrayList<RowObject> selectedItemArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hope_page);
lvList = (ListView)findViewById(R.id.lvList);
setUpListView();
lvList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
for (int i = 0; i < selectedItemArray.size(); i++)
{
if (i == position){
selectedItemArray.get(position).setSelectedItem(true);
}else{
selectedItemArray.get(i).setSelectedItem(false);
}
}
adapter.notifyDataSetChanged();
}
});
}
private void setUpListView()
{
arrayList = new ArrayList<>();
selectedItemArray = new ArrayList<>();
for (int i = 0; i < 20; i++)
{
arrayList.add("Item "+(i+1));
selectedItemArray.add(new RowObject(i, false));
}
adapter = new NavigationViewAdapter(HomePageActivity.this, arrayList, selectedItemArray);
if (adapter != null)
{
lvList.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
}
这是我的适配器
public class NavigationViewAdapter extends BaseAdapter
{
Activity activity;
View itemView;
private LayoutInflater inflater;
private ViewHolder viewHolder;
ArrayList<String> arrayList;
ArrayList<RowObject> selectedItemArray;
public NavigationViewAdapter(Activity activity, ArrayList<String> arrayList, ArrayList<RowObject> selectedItemArray)
{
this.activity = activity;
this.arrayList = arrayList;
this.selectedItemArray = selectedItemArray;
inflater = (LayoutInflater)this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int position) {
return arrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
try
{
itemView = convertView;
if (convertView == null){
itemView = inflater.from(parent.getContext()).inflate(R.layout.main_activity_item, parent, false);
viewHolder = new ViewHolder();
//if this is first time then inflate view
viewHolder.itemName = (TextView)itemView.findViewById(R.id.itemName);
itemView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)itemView.getTag();
}
//set Data from ArrayList
viewHolder.itemName.setText(arrayList.get(position));
if (selectedItemArray.get(position).isSelectedItem()){
viewHolder.itemName.setBackgroundColor(activity.getResources().getColor(R.color.colorPrimaryDark));
}else{
viewHolder.itemName.setBackgroundColor(activity.getResources().getColor(R.color.colorPrimary));
}
notifyDataSetChanged();
return itemView;
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
public class ViewHolder
{
TextView itemName;
}
}
这是我的RowObject.java
public class RowObject
{
public int position;
boolean isSelectedItem;
public RowObject(int position, boolean isSelectedItem) {
this.position = position;
this.isSelectedItem = isSelectedItem;
}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
public boolean isSelectedItem() {
return isSelectedItem;
}
public void setSelectedItem(boolean isSelectedItem) {
this.isSelectedItem = isSelectedItem;
}
}
答案 0 :(得分:0)
答案 1 :(得分:0)
我认为这种类型的布局不是由android提供的,但你可以使用你的逻辑来修复。我认为您需要更改您的xml文件,因为您的列表视图宽度为match_parent
,因此您的整个屏幕已填满,而您的第二个子菜单未显示。请根据您的要求更改或修复。你的主要xml文件。
<强> main_activity.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<ListView
android:id="@+id/lvList"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="@color/colorPrimary"/>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/lvList"
android:layout_toEndOf="@+id/lvList">
<LinearLayout
android:id="@+id/llItemContainer"
android:layout_width="200dp"
android:layout_height="50dp"
android:orientation="vertical"
android:background="@color/input_login_hint">
</LinearLayout>
</ScrollView>
</RelativeLayout>
</LinearLayout>
在listview onItemClickListener()中添加一个方法。请看我的代码。
java文件。
public class NavigationActivity extends AppCompatActivity
{
ListView lvList;
LinearLayout llItemContainer;
ArrayList<String> arrayList;
NavigationViewAdapter adapter;
ArrayList<RowObject> selectedItemArray;
ArrayList<String> selectedSecondItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.navigation_activity);
lvList = (ListView)findViewById(R.id.lvList);
setUpListView();
lvList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
llItemContainer = (LinearLayout)findViewById(R.id.llItemContainer);
lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
for (int i = 0; i < selectedItemArray.size(); i++)
{
if (i == position){
selectedItemArray.get(position).setSelectedItem(true);
}else{
selectedItemArray.get(i).setSelectedItem(false);
}
}
adapter.notifyDataSetChanged();
//here you can pass your sub view item array
generateSubView(position);
}
});
}
private void setUpListView()
{
arrayList = new ArrayList<>();
selectedItemArray = new ArrayList<>();
for (int i = 0; i < 20; i++)
{
arrayList.add("Item "+(i+1));
selectedItemArray.add(new RowObject(i, false));
}
adapter = new NavigationViewAdapter(NavigationActivity.this, arrayList, selectedItemArray);
if (adapter != null)
{
lvList.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
private void generateSubView(int position)
{
try
{
selectedSecondItem = new ArrayList<>();
llItemContainer.removeAllViews();
for (int i = 0; i <= position; i++)
{
final TextView rowText = new TextView(NavigationActivity.this);
TableRow.LayoutParams paramsExample = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
147,2.0f);
rowText.setId(i);
rowText.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
rowText.setGravity(Gravity.CENTER_VERTICAL);
rowText.setTextColor(getResources().getColor(android.R.color.white));
paramsExample.setMargins(3, 3, 3, 3);
rowText.setPadding(25, 25, 25, 25);
rowText.setTextSize(18);
rowText.setText("SecondItem " + i);
rowText.setLayoutParams(paramsExample);
rowText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rowText.setBackgroundColor(getResources().getColor(R.color.colorAccent));
selectedSecondItem.add(rowText.getText().toString());
}
});
llItemContainer.addView(rowText);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
您从selectedSecondItem
arraylist。