我正在寻找 ExpandableListView 自动滚动到特定位置
I am looking for ExpandableListView Auto Scroll to particular position
我希望我的应用程序在用户从 RecyclerView 中选择特定组名称时自动滚动到 ExpandableList 中的特定组。我正在使用界面来处理来自 RecyclerView
的点击
这是我的 mainActivity 代码:
@Override
public void onItemClick(int position) {
menuListForRestaurant.setVisibility(View.GONE);
if (!menuOpenChecker){
menuOpenChecker=true;
}else{
menuOpenChecker=false;
}
//The above code gets execute except for the below one
listView.smoothScrollToPosition(position);// this line is not executing
}
@SuppressLint("ClickableViewAccessibility")
private void ListViewAdapter(final HashMap<String, ArrayList<ProductList>> objectMap, final ArrayList<String> headerList) {
listViewAdapter= new RestaurantItemListView(RestaurantDetail.this.getApplicationContext(), objectMap,headerList);
listView.setAdapter(listViewAdapter);
listViewAdapter.notifyDataSetChanged();
}
private void RecyclerMenuView(ArrayList<String> headerList){
menuItemRecyclerView.setLayoutManager(new LinearLayoutManager(this));
menuItemRecyclerView.getRecycledViewPool().clear();
menuAdapterRecyclerView= new RestaurantMenuRecyclerView(headerList, this.getApplicationContext());
menuItemRecyclerView.setItemAnimator(new DefaultItemAnimator());
menuItemRecyclerView.setAdapter(menuAdapterRecyclerView);
menuAdapterRecyclerView.notifyDataSetChanged();
menuAdapterRecyclerView.setOnClick(RestaurantDetail.this);
}
它就像我提到的那行不起作用,因为它没有在 ExpandableListView 上更新
提前谢谢大家吗??
由于您正在使用 NonScrollExpandableListView,因此 ExpandableListView 不会滚动。在这里,我展示了 3 种可能的解决方案:
NonScrollExpandableListView 和 ScrollView(您当前的做法):您必须滚动 ScrollView,而不是滚动 ExpandableListView。如果展开的ExpandableListView中有任何组,则计算滚动位置将非常困难。因此,折叠所有组然后仅展开所需的组要容易得多。代码是这样的(我没测试过):
for (int i = 0; i < listViewAdapter.getGroupCount(); i++) listView.collapseGroup(i);
int a = header.getHeight(); // header refers to all views above the ExpandableListView.
a += (listViewAdapter.getGroupView(0, false, null, null).getHeight() + listView.getDividerHeight())*
position;
scrollView.smoothScrollTo(0, a);
listView.expandGroup(position);
2.ExpandableListView with header (NoScrollView): 开始一个新项目并尝试这个示例,验证它是否满足您的要求。这里我使用了 Spinner 而不是 RecyclerView。
MainActivity.java:
public class MainActivity extends AppCompatActivity {
LinkedHashMap<String, ArrayList<String>> dataList;
ExpandableListView expandableListView;
CustomBaseExpandableListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dataList = createSampleList();
expandableListView = findViewById(R.id.expandable_list_view);
adapter = new CustomBaseExpandableListAdapter(getApplicationContext(), dataList);
expandableListView.setAdapter(adapter);
View header = getLayoutInflater().inflate(R.layout.header, null);
Button btExpand = header.findViewById(R.id.bt_expand);
btExpand.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i = 0; i < adapter.getGroupCount(); i++) expandableListView.expandGroup(i);
}
});
Spinner spinner = findViewById(R.id.selector);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(getApplication(), android.R.layout.simple_list_item_1,
new ArrayList<>(dataList.keySet()));
spinner.setAdapter(arrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
int a = 1; // Without header a=0, with header a=1.
for (int j = 0; j < i; j++) {
a += (expandableListView.isGroupExpanded(j) ? adapter.getChildrenCount(j) : 0) + 1;
}
// expandableListView.smoothScrollToPosition(a);
expandableListView.smoothScrollToPositionFromTop(a, 0);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
expandableListView.addHeaderView(header);
}
private LinkedHashMap<String, ArrayList<String>> createSampleList() {
LinkedHashMap<String, ArrayList<String>> dataList = new LinkedHashMap<>();
for (int i = 0; i < 10; i++) {
ArrayList<String> childList = new ArrayList<>();
for (int j = 0; j < 17; j++) {
childList.add("Item " + i + "-" + j);
}
dataList.put("Group " + i, childList);
}
return dataList;
}
}
CustomBaseExpandableListAdapter.java:
public class CustomBaseExpandableListAdapter extends BaseExpandableListAdapter {
ArrayList<String> groupList;
LinkedHashMap<String, ArrayList<String>> dataList;
Context context;
public CustomBaseExpandableListAdapter(Context context, LinkedHashMap<String, ArrayList<String>> dataList) {
this.context = context;
this.dataList = dataList;
groupList = new ArrayList(dataList.keySet());
}
@Override
public int getGroupCount() {
return groupList.size();
}
@Override
public int getChildrenCount(int i) {
return dataList.get(groupList.get(i)).size();
}
@Override
public String getGroup(int i) {
return groupList.get(i);
}
@Override
public String getChild(int i, int i1) {
return dataList.get(groupList.get(i)).get(i1);
}
@Override
public long getGroupId(int i) {
return 0;
}
@Override
public long getChildId(int i, int i1) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
TextView textView;
if (view == null) {
textView = new TextView((context));
textView.setTextSize(30);
textView.setPadding(80, 0, 0, 0);
} else {
textView = (TextView) view;
}
textView.setText(getGroup(i));
return textView;
}
@Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
TextView textView;
if (view == null) {
textView = new TextView((context));
textView.setTextSize(20);
textView.setPadding(120, 0, 0, 0);
} else {
textView = (TextView) view;
}
textView.setText(getChild(i, i1));
return textView;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Spinner
android:id="@+id/selector"
android:layout_marginLeft="10dp"
android:layout_width="match_parent"
android:layout_height="50dp"/>
<ExpandableListView
android:id="@+id/expandable_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
header.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
app:srcCompat="@drawable/ic_launcher_foreground" />
<Button
android:id="@+id/bt_expand"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="+"
android:textSize="30dp" />
</LinearLayout>
- ExpandableListView using ViewPager as child view (ExpandableListView with ViewPager combination as its child), ExpandableListView using HorizontalRecyclerView as child view (Expandable list view with HorizontalScroll View for child view): 使用其中一种方法并将ExpandableListView的
OnGroupExpandListener
设置为仅展开一组,那么我认为当前的RecyclerView是不必要的.
我终于使用下面的简单代码解决了问题:
//AutoScroll for menu
int t = 0,j, e=0, l=0;
for (int n=0; n<=position;n++){
e= e +objectMap.get(textHeaderlist.get(n)).size();
l= l+450;
}
t=position+1;
j= 200+e+t+l;
scrollView.scrollTo(5, j);
终于解决了我的问题。
无论如何,谢谢!
我希望我的应用程序在用户从 RecyclerView 中选择特定组名称时自动滚动到 ExpandableList 中的特定组。我正在使用界面来处理来自 RecyclerView
的点击这是我的 mainActivity 代码:
@Override
public void onItemClick(int position) {
menuListForRestaurant.setVisibility(View.GONE);
if (!menuOpenChecker){
menuOpenChecker=true;
}else{
menuOpenChecker=false;
}
//The above code gets execute except for the below one
listView.smoothScrollToPosition(position);// this line is not executing
}
@SuppressLint("ClickableViewAccessibility")
private void ListViewAdapter(final HashMap<String, ArrayList<ProductList>> objectMap, final ArrayList<String> headerList) {
listViewAdapter= new RestaurantItemListView(RestaurantDetail.this.getApplicationContext(), objectMap,headerList);
listView.setAdapter(listViewAdapter);
listViewAdapter.notifyDataSetChanged();
}
private void RecyclerMenuView(ArrayList<String> headerList){
menuItemRecyclerView.setLayoutManager(new LinearLayoutManager(this));
menuItemRecyclerView.getRecycledViewPool().clear();
menuAdapterRecyclerView= new RestaurantMenuRecyclerView(headerList, this.getApplicationContext());
menuItemRecyclerView.setItemAnimator(new DefaultItemAnimator());
menuItemRecyclerView.setAdapter(menuAdapterRecyclerView);
menuAdapterRecyclerView.notifyDataSetChanged();
menuAdapterRecyclerView.setOnClick(RestaurantDetail.this);
}
它就像我提到的那行不起作用,因为它没有在 ExpandableListView 上更新
提前谢谢大家吗??
由于您正在使用 NonScrollExpandableListView,因此 ExpandableListView 不会滚动。在这里,我展示了 3 种可能的解决方案:
NonScrollExpandableListView 和 ScrollView(您当前的做法):您必须滚动 ScrollView,而不是滚动 ExpandableListView。如果展开的ExpandableListView中有任何组,则计算滚动位置将非常困难。因此,折叠所有组然后仅展开所需的组要容易得多。代码是这样的(我没测试过):
for (int i = 0; i < listViewAdapter.getGroupCount(); i++) listView.collapseGroup(i); int a = header.getHeight(); // header refers to all views above the ExpandableListView. a += (listViewAdapter.getGroupView(0, false, null, null).getHeight() + listView.getDividerHeight())* position; scrollView.smoothScrollTo(0, a); listView.expandGroup(position);
2.ExpandableListView with header (NoScrollView): 开始一个新项目并尝试这个示例,验证它是否满足您的要求。这里我使用了 Spinner 而不是 RecyclerView。
MainActivity.java:
public class MainActivity extends AppCompatActivity {
LinkedHashMap<String, ArrayList<String>> dataList;
ExpandableListView expandableListView;
CustomBaseExpandableListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dataList = createSampleList();
expandableListView = findViewById(R.id.expandable_list_view);
adapter = new CustomBaseExpandableListAdapter(getApplicationContext(), dataList);
expandableListView.setAdapter(adapter);
View header = getLayoutInflater().inflate(R.layout.header, null);
Button btExpand = header.findViewById(R.id.bt_expand);
btExpand.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i = 0; i < adapter.getGroupCount(); i++) expandableListView.expandGroup(i);
}
});
Spinner spinner = findViewById(R.id.selector);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(getApplication(), android.R.layout.simple_list_item_1,
new ArrayList<>(dataList.keySet()));
spinner.setAdapter(arrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
int a = 1; // Without header a=0, with header a=1.
for (int j = 0; j < i; j++) {
a += (expandableListView.isGroupExpanded(j) ? adapter.getChildrenCount(j) : 0) + 1;
}
// expandableListView.smoothScrollToPosition(a);
expandableListView.smoothScrollToPositionFromTop(a, 0);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
expandableListView.addHeaderView(header);
}
private LinkedHashMap<String, ArrayList<String>> createSampleList() {
LinkedHashMap<String, ArrayList<String>> dataList = new LinkedHashMap<>();
for (int i = 0; i < 10; i++) {
ArrayList<String> childList = new ArrayList<>();
for (int j = 0; j < 17; j++) {
childList.add("Item " + i + "-" + j);
}
dataList.put("Group " + i, childList);
}
return dataList;
}
}
CustomBaseExpandableListAdapter.java:
public class CustomBaseExpandableListAdapter extends BaseExpandableListAdapter {
ArrayList<String> groupList;
LinkedHashMap<String, ArrayList<String>> dataList;
Context context;
public CustomBaseExpandableListAdapter(Context context, LinkedHashMap<String, ArrayList<String>> dataList) {
this.context = context;
this.dataList = dataList;
groupList = new ArrayList(dataList.keySet());
}
@Override
public int getGroupCount() {
return groupList.size();
}
@Override
public int getChildrenCount(int i) {
return dataList.get(groupList.get(i)).size();
}
@Override
public String getGroup(int i) {
return groupList.get(i);
}
@Override
public String getChild(int i, int i1) {
return dataList.get(groupList.get(i)).get(i1);
}
@Override
public long getGroupId(int i) {
return 0;
}
@Override
public long getChildId(int i, int i1) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
TextView textView;
if (view == null) {
textView = new TextView((context));
textView.setTextSize(30);
textView.setPadding(80, 0, 0, 0);
} else {
textView = (TextView) view;
}
textView.setText(getGroup(i));
return textView;
}
@Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
TextView textView;
if (view == null) {
textView = new TextView((context));
textView.setTextSize(20);
textView.setPadding(120, 0, 0, 0);
} else {
textView = (TextView) view;
}
textView.setText(getChild(i, i1));
return textView;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Spinner
android:id="@+id/selector"
android:layout_marginLeft="10dp"
android:layout_width="match_parent"
android:layout_height="50dp"/>
<ExpandableListView
android:id="@+id/expandable_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
header.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
app:srcCompat="@drawable/ic_launcher_foreground" />
<Button
android:id="@+id/bt_expand"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="+"
android:textSize="30dp" />
</LinearLayout>
- ExpandableListView using ViewPager as child view (ExpandableListView with ViewPager combination as its child), ExpandableListView using HorizontalRecyclerView as child view (Expandable list view with HorizontalScroll View for child view): 使用其中一种方法并将ExpandableListView的
OnGroupExpandListener
设置为仅展开一组,那么我认为当前的RecyclerView是不必要的.
我终于使用下面的简单代码解决了问题:
//AutoScroll for menu
int t = 0,j, e=0, l=0;
for (int n=0; n<=position;n++){
e= e +objectMap.get(textHeaderlist.get(n)).size();
l= l+450;
}
t=position+1;
j= 200+e+t+l;
scrollView.scrollTo(5, j);
终于解决了我的问题。
无论如何,谢谢!