强制ExpandableListView fastScroll遍历所有项目而不是部分

时间:2018-08-07 12:25:54

标签: android expandablelistview expandablelistadapter fastscroll

我正在使用ExpandableListViewBaseExpandableListAdapter,并且我注意到启用fastScroll可以使滚动遍历节或标题。如果不是因为它不能正常工作,那就太好了。 fastScroll遍历所有部分,只有一小部分滚动在顶部,只是开始滚动,然后在滚动的第一季度通过并且所有部分都滚动到屏幕时,滚动没有任何作用

这是一个示范gif:

Demonstration GIF

我检查了这则旧帖子android fastScroll only covers part of the list,但对我不起作用。

有人知道此错误的另一种解决方法,还是知道如何强制ExpandableListView fastScroll遍历所有项而不是节?

如果对您有用,这里是示例代码。

activity_main.xml

<FrameLayout
  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=".MainActivity">

  <ExpandableListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:fastScrollEnabled="true"
    android:layout_height="match_parent"></ExpandableListView>

</FrameLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

 private ExpandableListView listView;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   listView = findViewById(R.id.listview);

   ExpandableAdapter adapter = new ExpandableAdapter(this, getItems());
   listView.setAdapter(adapter);
 }

 private Map<String, List<String>> getItems() {
   Map<String, List<String>> items = new LinkedHashMap<>();
   // Loops to create stub items
   return items;
 }

 private static class ExpandableAdapter extends BaseExpandableListAdapter {

  private final Context context;
  private ArrayList<String> sections;
  private Map<String, List<String>> items;

  public ExpandableAdapter(Context context, Map<String, List<String>> items) {
    this.context = context;
    this.items = items;
    this.sections = new ArrayList<>(items.keySet());
  }

  @Override
  public int getGroupCount() {
    return sections.size();
  }

  @Override
  public int getChildrenCount(int groupPosition) {
    return items.get(sections.get(groupPosition)).size();
  }

  @Override
  public String getGroup(int groupPosition) {
    return sections.get(groupPosition);
  }

  @Override
  public String getChild(int groupPosition, int childPosition) {
    return items.get(getGroup(groupPosition)).get(childPosition);
  }

  @Override
  public long getGroupId(int groupPosition) {
    return 0;
  }

  @Override
  public long getChildId(int groupPosition, int childPosition) {
    return 0;
  }

  @Override
  public boolean hasStableIds() {
    return false;
  }

  @Override
  public View getGroupView(int groupPosition, boolean b, View view, ViewGroup viewGroup) {
    // No ViewHolder pattern because of testing purposes
    view = View.inflate(context, R.layout.row, null);
    TextView tv = view.findViewById(R.id.text);
    tv.setText(getGroup(groupPosition));
    return view;
  }

  @Override
  public View getChildView(int groupPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) {
    // No ViewHolder pattern because of testing purposes
    view = View.inflate(context, R.layout.row, null);
    TextView tv = view.findViewById(R.id.text);
    tv.setText(getChild(groupPosition, childPosition));
    return view;
  }

  @Override
  public boolean isChildSelectable(int groupPosition, int childPosition) {
    return false;
  }
 }
}

0 个答案:

没有答案