Android定点刷新Listview(Gridview)Item!实现指定Item刷新

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/GodDavide/article/details/53067403
最近在做Launcher,定制了拖拽互换应用图标位置,但在拖拽过程中,由于 notifyDataSetChanged();次数太多,造成拖拽卡顿不流畅,所以用到了定点刷新,只刷新指定的Item。
理论很简单,就是得到要刷新的Item的View,然后更改该View即可,下面上代码:

效果图:
这里写图片描述

             start

step1:布局文件

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:background="#fff"
    tools:context="com.god.reflashviewitem.MainActivity">

    <EditText
        android:id="@+id/main_ed"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginTop="40dp"
        android:hint="仅限输入数字,刷新该Item"
        android:inputType="number"
        android:textColor="#f0001c"
        android:textColorHint="#00FF00"
        android:windowSoftInputMode="adjustUnspecified|stateHidden" />

    <TextView
        android:id="@+id/main_aline"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerHorizontal="true" />

    <Button
        android:id="@+id/main_bt_refresh"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/main_ed"
        android:layout_marginRight="50dp"
        android:layout_toLeftOf="@id/main_aline"
        android:background="@null"
        android:onClick="onClick"
        android:text="定点刷新"
        android:textColor="#070b9c" />

    <Button
        android:id="@+id/main_bt_reset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/main_ed"
        android:layout_marginLeft="50dp"
        android:layout_toRightOf="@id/main_aline"
        android:background="@null"
        android:onClick="onClick"
        android:text="重置所有"
        android:textColor="#070b9c" />

    <GridView
        android:id="@+id/main_listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/main_bt_refresh"
        android:layout_marginTop="20dp"
        android:numColumns="2" />
</RelativeLayout>

step2:写Adapter

package com.god.reflashviewitem;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

/**
 * @author David  create on 2016/11/3  15:24.
 * @email david.forever.god@gmail.com
 * Learn from yesterday, live for today, hope for tomorrow.
 */
public class GridviewAdapter extends BaseAdapter {

    private Context context;
    private List<GridBean> data;
    private ViewHolder holder;

    public GridviewAdapter(Context context, List<GridBean> data) {
        this.context = context;
        this.data = data;
    }

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

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.grid_item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = ((ViewHolder) convertView.getTag());
        }
        viewHolder.item_iv.clearAnimation();
        setUpView(position, convertView, viewHolder);
        return convertView;
    }

    private void setUpView(int position, View convertView, ViewHolder viewHolder) {
        GridBean gridBean = data.get(position);
        if (gridBean.getDrawable() != null) {
            viewHolder.item_iv.setImageDrawable(gridBean.getDrawable());
        }
        if (gridBean.getName() != null) {
            viewHolder.item_tv.setText(gridBean.getName());
        }
    }

//关键方法,更新指定Item
    public void updateItem(int refreshItemPostion, final View view) {
        if (refreshItemPostion > data.size()) {
            Toast.makeText(context, "error:超出列表范围", Toast.LENGTH_SHORT).show();
            return;
        }
        holder = (ViewHolder) view.getTag();
        holder.item_tv.setText("定点刷新成功");
        holder.item_iv.setImageDrawable(context.getResources().getDrawable(R.drawable.lol_refresh));
        startAnimaSet(view);//添加了动画效果,根据需求而定
    }

    private AnimationSet animationSet;

    private void startAnimaSet(View view) {
        ScaleAnimation scaleAnimation = new ScaleAnimation(0.5f, 1, 0.5f, 1,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        AlphaAnimation alphaAnimation = new AlphaAnimation(0.4f, 1f);
        animationSet = new AnimationSet(true);
        animationSet.addAnimation(scaleAnimation);
        animationSet.addAnimation(alphaAnimation);
        animationSet.setDuration(350);
        view.startAnimation(animationSet);
    }

    class ViewHolder {
        private ImageView item_iv;
        private TextView item_tv;

        public ViewHolder(View v) {
            item_iv = ((ImageView) v.findViewById(R.id.item_iv));
            item_tv = ((TextView) v.findViewById(R.id.item_tv));
        }
    }
}

Itme布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/item_iv"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_gravity="center"
        android:scaleType="fitXY"
        android:background="@null" />

    <TextView
        android:id="@+id/item_tv"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:background="@null"
        android:gravity="center"
        android:textColor="#00e"
        android:layout_gravity="center"
        />

</LinearLayout>

step3:MainActivity

package com.god.reflashviewitem;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private GridView gridview;
    private List<GridBean> gridBeen;
    private EditText main_ed;
    private GridviewAdapter gridviewAdapter;
    private GridBean lol_01;
    private int refreshPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initview();
        creatList();
        initData();
        gridviewAdapter = new GridviewAdapter(this, gridBeen);
        gridview.setAdapter(gridviewAdapter);
    }

    private void initview() {
        gridview = (GridView) findViewById(R.id.main_listview);
        main_ed = (EditText) findViewById(R.id.main_ed);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.main_bt_refresh://定点刷新
                if (main_ed != null) {
                    String s = main_ed.getText().toString();
                    refreshPosition = Integer.parseInt(s);
                }
                View childAt = gridview.getChildAt(refreshPosition);
                gridviewAdapter.updateItem(refreshPosition, childAt);
                Toast.makeText(MainActivity.this, "已刷新", Toast.LENGTH_SHORT).show();
                break;
            case R.id.main_bt_reset://重置所有
                initData();
                gridviewAdapter.notifyDataSetChanged();
                Toast.makeText(MainActivity.this, "已重置", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    private void initData() {
        if (gridBeen != null) {
            gridBeen.clear();
        } else {
            gridBeen = new ArrayList<>();
        }
        for (int i = 0; i < 10; i++) {
            gridBeen.add(lol_01);
        }
    }

    private void creatList() {
        Drawable drawable1 = getResources().getDrawable(R.drawable.lol1);
        lol_01 = new GridBean("lol_01", drawable1);
    }
}
                             end

@Auther: David

@email :david.forever.god@gmail.com

Learn from yesterday, live for today, hope for tomorrow.

展开阅读全文

没有更多推荐了,返回首页