我的Android活动锻炼视图显示嵌套列表视图。父列表的每个对象都显示和练习名称和2个按钮以及子列表视图。第一个父列表元素不显示子项,第二个显示第一个子列表的子列表,第三个显示秒子项。如果你上下猛烈地滚动,第一个父母将显示第三个孩子列表。
在开始时看起来像这样:
滚动暴力之后,它看起来像这样:
我的XML有三个文件
activity_workout_view.xml
是第一个:
<RelativeLayout 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"
tools:context="com.example.franzene.workoutplanner.WorkoutView">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="1dp"
android:horizontalSpacing="1dp"
android:stretchMode="columnWidth"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
id / listview使用activity_workout_view_listview.xml
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/list_item_string"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:paddingLeft="8dp"
android:textSize="18sp"
android:textStyle="bold"
/>
<Button
android:id="@+id/delete_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_marginRight="0dp"
android:text="Dele"
/>
<Button
android:id="@+id/add_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toLeftOf="@id/delete_btn"
android:layout_marginRight="0dp"
android:text="Add"
/>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnWidth="200dp"
android:verticalSpacing="1dp"
android:horizontalSpacing="1dp"
android:stretchMode="columnWidth"
android:layout_below="@id/delete_btn"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
WorkoutViewAdapterListView.java是父列表的适配器,包含子列表的适配器:
public class WorkoutViewAdapterListView extends BaseAdapter {
DatabaseHelper db;
private Context context;
// private String[] text = {"hi","d"};
private int testInt = 0;
List<WorkoutExercise> WE_TABLE;
MyThumbnaildapter thadapter = null;
public WorkoutViewAdapterListView(Context context, int i) {
this.context = context;
testInt = i;
db = new DatabaseHelper(context);
WE_TABLE = db.getWorkoutExercise(i + "");
db.closeDB();
}
public int getCount() {
//Log.e("Test" , "COUNT: " + totalSetCount + " + " + WE_TABLE.size() );
return WE_TABLE.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return position;
}
private int[] getLocation(int i) {
db = new DatabaseHelper(context);
int temp[] = {-1, -1};
int count = -1;
for (int we = 0; we < WE_TABLE.size(); we++) {
for (int s = 0; s < db.getSetCountByWEID(WE_TABLE.get(we).getID()) + 1; s++) {
count++;
if (count == i) {
//Log.e("Test","WE: " + we+ " S: " + s + " For: " + i);
temp[0] = we;
temp[1] = s;
db.closeDB();
return temp;
}
}
}
db.closeDB();
return temp;
}
public class MyThumbnaildapter extends ArrayAdapter<SetTable> {
ArrayList<SetTable> arr;
private TextView text;
public MyThumbnaildapter(Context context, int textViewResourceId, ArrayList<SetTable> arr) {
super(context, textViewResourceId, arr);
this.arr = arr;
}
public int getCount() {
return arr.size();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
TextView tv;
View view = convertView;
// if (view == null) {
Log.e("Test","VIEWPOSITION:" + position);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.activity_workout_view_gridview_set, null);
Button delButton = (Button) view.findViewById(R.id.delete_btn2);
delButton.setTag(position);
tv = new TextView(context);
tv.setLayoutParams(new ListView.LayoutParams(ListView.LayoutParams.FILL_PARENT, ListView.LayoutParams.WRAP_CONTENT));
tv = (TextView) view.findViewById(R.id.list_item_string2);
delButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Test","buttonclick:" + position);
int id = (Integer) v.getTag();
arr.remove(id);
notifyDataSetChanged();
}
});
tv.setText(arr.get(position).getWeight() + "lbs " + arr.get(position).getReps() + "times ");
Log.e("Test", "view text:" + tv.getText());
view.setBackgroundColor(Color.parseColor("#FE642E"));
//}
return view;
}
}
public View getView(final int position, View convertView, ViewGroup parent) {
TextView tv;
View view = convertView;
ListView NumberList;
// if (view == null) {
db = new DatabaseHelper(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//view.setBackgroundDrawable(ColorDrawable("#e74c3c");
// LinearLayout linearLayout = (LinearLayout)(R.id.layout);
view = inflater.inflate(R.layout.activity_workout_view_listview, null);
tv = new TextView(context);
tv.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.FILL_PARENT, GridView.LayoutParams.WRAP_CONTENT));
Button deleteBtn = (Button) view.findViewById(R.id.delete_btn);
ArrayList<SetTable> S_TABLE = db.getSets(WE_TABLE.get(position).getID() + "");
ListView listView = (ListView) view.findViewById(R.id.listview2);
listView.setAdapter(thadapter);
ViewGroup.LayoutParams layoutParams = listView.getLayoutParams();
int h = listView.getCount() * listView.getMeasuredHeight();
layoutParams.height = h;
listView.setLayoutParams(layoutParams);
thadapter = new MyThumbnaildapter(context, R.layout.activity_workout_view_gridview_set, S_TABLE);
int totalHeight = 0;
for (int i = 0; i < thadapter.getCount(); i++) {
View mView = thadapter.getView(i, null, listView);
mView.measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
totalHeight += mView.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (thadapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
tv = (TextView) view.findViewById(R.id.list_item_string);
//Log.e("Test", "Location: " + location[0] + " , " + location[1] + " Number " + Obj);
String Exercises = WE_TABLE.get(position).getExerciseName();
view.setBackgroundColor(Color.parseColor("#e74c3c"));
//deleteBtn.setVisibility(View.GONE);
tv.setText(Exercises);
db.closeDB();
//}
return view;
}
}
答案 0 :(得分:0)
您不应在ListView项目中使用ListView。
看看http://developer.android.com/reference/android/widget/ExpandableListView.html 这似乎是你在寻找的。 p>
答案 1 :(得分:0)
我可能错了,但列表中的列表设计并不是很好。当您快速滚动时,您可能会在回收视图中遇到大量争用。请记住,每次滚动时,“getView”都会被调用到该列表的回收视图。然后,对于每个项目,它需要调用第二个列表的getView并回收所有这些视图..
此外,这将导致一些焦点问题。当您单击第二个“嵌套”列表中的按钮时...是否单击了嵌套列表按钮?第二个清单的清单项目?或者父列表的列表项?我希望你能看到你会遇到一些问题。
我可能建议您进行设置以便进行设置,以便单击一个列表项,该列表项转换为另一个活动,其中包含您执行的每个练习的单个列表视图?
除此之外......我注意到你没有使用正确的设计模式来获取视图。你在每次调用get view时都在膨胀新的布局。这是一个令人难以置信的资源困难...我可以想象它对于嵌套列表视图会很糟糕。您应该使用视图持有者并检查视图是否为空。如果它为null,则根据需要扩展布局并获取其他资源。然后,如果它不为null,则应从视图持有者中提取引用。
阅读本文档,了解如何合并此设计模式。 https://www.codeofaninja.com/2013/09/android-viewholder-pattern-example.html