android – GridView内部的GridView具有滞后滚动功能

android – GridView内部的GridView具有滞后滚动功能,第1张

概述我在PageViewer中实现了一个GridView,但是滚动并不像它应该的那样平滑. GridView内部的视图正在拦截触摸. 这让我抓狂,我已经尝试了所有的东西,虽然有一件事情起作用,但最终并不是一个好的解决方案.首先是我的代码. apps_grid.xml <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" 我在PageVIEwer中实现了一个GrIDVIEw,但是滚动并不像它应该的那样平滑. GrIDVIEw内部的视图正在拦截触摸.

这让我抓狂,我已经尝试了所有的东西,虽然有一件事情起作用,但最终并不是一个好的解决方案.首先是我的代码.

apps_grID.xml

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:orIEntation="vertical">    <GrIDVIEw        androID:ID="@+ID/appGrID"        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:background="@drawable/ic_launcher_background"        androID:horizontalSpacing="4dp"        androID:numColumns="4"        androID:paddingtop="10dp"        androID:stretchMode="columnWIDth"        androID:verticalSpacing="15dp"/></linearLayout>

app_item.xml

<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:orIEntation="vertical">    <ImageVIEw        androID:ID="@+ID/icon"        androID:layout_wIDth="54dp"        androID:layout_height="54dp"        androID:layout_gravity="center_horizontal"        androID:src="@mipmap/ic_launcher" />    <TextVIEw        androID:ID="@+ID/text"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_gravity="center_horizontal"        androID:layout_margintop="5dp"        androID:ellipsize="end"        androID:gravity="center"        androID:paddingEnd="15dp"        androID:paddingStart="15dp"        androID:singleline="true"        androID:text="Title"        androID:textcolor="@androID:color/white"        androID:textSize="14sp" /></linearLayout>

activity_main.xml中

<androID.support.v4.vIEw.VIEwPager    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:ID="@+ID/vIEwpager"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:layout_margintop="45dp"    androID:fillVIEwport="true"    app:layout_behavior="@string/appbar_scrolling_vIEw_behavior"    />

AppModel.java

public class AppModel {    private String label;    private String pkg;    private Drawable mIcon;    public AppModel(String name,String pkg,Drawable icon) {        this.label = name;        this.pkg = pkg;        this.mIcon = icon;    }    public AppModel(String name) {        this.label = name;    }    public String getLabel() {        return this.label;    }    public String getPkg() {        return this.pkg;    }    public Drawable getIcon() {        return mIcon;    }}

Applistadapter.java

public class Applistadapter extends BaseAdapter {    private ArrayList<AppModel> apps;    private LayoutInflater layoutInflater;    public Applistadapter(Context context,ArrayList<AppModel> apps) {        this.apps = apps;        this.layoutInflater = LayoutInflater.from(context);    }    @OverrIDe    public int getCount() {        return apps.size();    }    @OverrIDe    public long getItemID(int position) {        return 0;    }    @OverrIDe    public Object getItem(int position) {        AppModel app = apps.get(position);        if(app == null) {            return null;        }        return app;    }    @OverrIDe    public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) {        AppModel app = apps.get(position);        if (convertVIEw == null) {            convertVIEw = layoutInflater.inflate(R.layout.app_item,null);        }        if(app == null) {            return convertVIEw;        }        ((TextVIEw)convertVIEw.findVIEwByID(R.ID.text)).setText(app.getLabel());        return convertVIEw;    }}

TestPagerAdapter.java

public class TestPagerAdapter extends PagerAdapter {    private Context mContext;    private LayoutInflater inflater;    private ArrayList<AppModel> apps;    private int perPage = 12;    public TestPagerAdapter(Context context) {        this.mContext = context;        this.inflater = LayoutInflater.from(context);    }    @OverrIDe    public Object instantiateItem(VIEwGroup parent,int position) {        VIEwGroup layout = (VIEwGroup) inflater.inflate(R.layout.apps_grID,parent,false);        ArrayList<AppModel> pageApps = getAppsForPage(position);        if(pageApps == null) {            return layout;        }        final GrIDVIEw appGrID = (GrIDVIEw) layout.findVIEwByID(R.ID.appGrID);        final Applistadapter grIDAdapter = new Applistadapter(mContext,pageApps);        appGrID.setVerticalScrollbarEnabled(false);        appGrID.setAdapter(grIDAdapter);        parent.addVIEw(layout);        return layout;    }    @OverrIDe    public voID destroyItem(VIEwGroup parent,int position,Object vIEw) {        parent.removeVIEw((VIEw) vIEw);    }    @OverrIDe    public int getCount() {        if(apps != null) {            return (int) Math.ceil((double) apps.size() / perPage);        } else {            return 0;        }    }    @OverrIDe    public boolean isVIEwFromObject(VIEw vIEw,Object object) {        return vIEw == object;    }    public voID setData(ArrayList<AppModel> data) {        if(data != null) {            this.apps = data;        }    }    public ArrayList<AppModel> getAppsForPage(int page) {        if(apps == null) {            return null;        }        int size = apps.size();        int offset = getoffset(page);        ArrayList<AppModel> pageApps = new ArrayList<AppModel>(size);        for(int i = 0; i < perPage; i++) {            if(offset+i < size) {                pageApps.add(apps.get(offset+i));            }        }        return pageApps;    }    public int getoffset(int page) {        return perPage*page;    }}

MainActivity.java

@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    VIEwPager vIEwPager = (VIEwPager) findVIEwByID(R.ID.vIEwpager);    TestPagerAdapter mPAdapter = new TestPagerAdapter(this);    vIEwPager.setAdapter(mPAdapter);    ArrayList<AppModel> items = new ArrayList<AppModel>(20);    for(int i=0;i<20;i++) {        AppModel app = new AppModel(""+i);        items.add(app);    }    mPAdapter.setData(items);    mPAdapter.notifyDataSetChanged();}

所以,这是我尝试过的:
我用RecyclerVIEw替换了GrIDVIEw.
在EVERY VIEw上添加了所有这些属性

androID:clickable="false"androID:contextClickable="false"androID:defaultFocusHighlightEnabled="false"androID:focusable="false"androID:focusableIntouchMode="false"androID:focusedByDefault="false"androID:isScrollContainer="false"androID:longClickable="false"androID:nestedScrollingEnabled="false"androID:overScrollMode="never"androID:touchscreenBlocksFocus="false"

在GrIDVIEw上覆盖OntouchListener(以及许多其他)并返回true.
可能尝试了一些我现在记不住的其他事情了.

唯一有效的是在GrIDVIEw上设置androID:filtertouchesWhenObscured =“true”属性,而其他一些应用程序视图位于顶部(所以条件“WhenObscured”满足)
当我尝试这个时,我的VIEwPager开始非常流畅地滚动,在其他情况下它有时不会滚动(可能是当GrIDVIEw拦截它时).

请注意,GrIDVIEw中的项目必须是可点击的.

UPDATE

https://github.com/mpa4hu/GridViewPager这是项目的github链接,用类似于这张图片的手势模拟向前和向后滚动的问题

解决方法 Recycler视图会回收视图,并且在需要之前不会重新创建视图.它只是重新绑定视图.

RecyclerVIEw是作为ListVIEw改进创建的,所以是的,您可以使用ListVIEw控件创建附加列表,但使用RecyclerVIEw更容易:

在向上/向下滚动时重用单元格 – 这可以通过在ListVIEw适配器中实现VIEw Holder来实现,但它是可选的,而在RecycleVIEw中它是编写适配器的默认方式.

将列表与其容器分离 – 因此您可以使用设置LayoutManager在运行时轻松地将列表项放在不同的容器(linearLayout,grIDLayout)中.

例:

mRecyclerVIEw = (RecyclerVIEw) findVIEwByID(R.ID.my_recycler_vIEw);mRecyclerVIEw.setLayoutManager(new linearlayoutmanager(this));//ormRecyclerVIEw.setLayoutManager(new GrIDLayoutManager(this,2));//number of columns in a grID layout

有关RecyclerVIEw的更多信息,但我认为这些是主要的.

因此,总而言之,RecyclerVIEw是一种更灵活的控制,用于处理“列表数据”,遵循关注委托模式,并为自己留下一个任务 – 回收项目.

你可以在这里参考它的优点:RecyclerView over ListView

总结

以上是内存溢出为你收集整理的android – GridView内部的GridView具有滞后滚动功能全部内容,希望文章能够帮你解决android – GridView内部的GridView具有滞后滚动功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1130831.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存