包含图片的列表视图中的效果不佳

时间:2018-10-22 12:04:44

标签: android listview android-image

在打开此标题之前,我做了很多研究,但不知何故我没有得到想要的结果。我的Listview加速了,但是还不够。

当我上下调整列表视图时,有时它会变慢并且霜很少。

这是因为Listview仅包含图片吗?但是当我检查其他应用程序时,发现它们没有问题。

请您复习一下?

这是我的适配器代码:

public class pop_Adapter extends BaseAdapter  {
    private LayoutInflater userInflater;
    private List<pop_User> userList;
    Activity aa;
    public pop_Adapter(Activity activity, List<pop_User> userList) {
        userInflater = (LayoutInflater) activity.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        aa = activity;
        this.userList = userList;
    }

    @Override
    public int getCount() {
        return userList.size();
    }

    @Override
    public Object getItem(int i) {
        return userList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    class ViewYerTutucu{

        RelativeLayout rela1;
        LinearLayout activity_main,lin1;
        TextView indir;
        ImageView imageViewUserPicture;

        ViewYerTutucu(View v){
            activity_main = (LinearLayout) v.findViewById(R.id.activity_main);
            indir = (TextView) v.findViewById(R.id.indirmeSayisi);
            imageViewUserPicture = (ImageView) v.findViewById(R.id.resim);
            lin1 = (LinearLayout) v.findViewById(R.id.lin1);

            rela1 = (RelativeLayout) v.findViewById(R.id.rela1);
            rela1.getLayoutParams().height = (int) ( (MainActivity.height / MainActivity.width) * MainActivity.width/2 );  //(int)MainActivity.width/2-36;
            indir.setTextSize(MainActivity.pxToDp((int)MainActivity.width/23));

        }

    }


    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {

        View lineView = view;

        ViewYerTutucu tutucu=null;

        if (lineView == null) {
            lineView = userInflater.inflate(R.layout.custom_pop, viewGroup, false);
            tutucu=new ViewYerTutucu(lineView);
            lineView.setTag(tutucu);
        }
        else{
            tutucu= (ViewYerTutucu) lineView.getTag();
        }

        final pop_User user = userList.get(i);

        tutucu.indir.setText(user.getIndirme());

        Glide.with(aa).load(user.getTags()).into(tutucu.imageViewUserPicture);

        final ViewYerTutucu finalTutucu = tutucu;
        tutucu.lin1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity.hangiBolum = "pop-"+(i*2);
                fragoyunsifir.ResimDegistir(user.getTags());
                fragoyunsifir.paylas.setTag(user.getId());
                fragoyunsifir.indir.setTag(user.getTags());
                fragoyunsifir.button1.setTag(user.getResim());
                Animation fadeOut = AnimationUtils.loadAnimation(aa, R.anim.fade_in_acilis);
                fragoyunsifir.activity_main.startAnimation(fadeOut);
                fadeOut.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        fragoyunsifir.gorunurYap();
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
            }
        });



        tutucu.indir.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(aa, finalTutucu.indir.getText()+" downloads", Toast.LENGTH_SHORT).show();
            }
        });

        if(reachedEndOfList(i)) loadMoreData();

        return lineView;
    }




    private boolean reachedEndOfList(int position) {
        // can check if close or exactly at the end
        return position == getCount() - 1;
    }

    private void loadMoreData() {

        MainActivity.News();

    }
}

已加载示例图片:https://source.unsplash.com/-hI5dX2ObAs/540x720

2 个答案:

答案 0 :(得分:2)

最好使用RecyclerView来平滑滚动和提高性能。看一下这个guide以便在您的应用中实现它。还可以考虑使用图像加载库,例如picasso。这是因为未启动具有高分辨率图像,需要花费一些时间才能下载。但是,图像加载器库允许您缓存和压缩图像,以便它们可以快速加载到视图中。

答案 1 :(得分:0)

为获得流畅的滚动效果和性能,请在ScrollView中添加Recyclerview并进行设置

 android:nestedScrollingEnabled="false"

这将加载recyclerview中的所有内容。否则,仅当您向下滚动时才加载。这就是我的想法。

并设置

    @Override
    public int getItemViewType(int position) {
    return position;
   }

在您的recyclerview适配器内部。
 这是为了防止图像的位置在您进一步向下滚动并到达顶部时发生变化。