长按实现控件拖拽换位

public class DragActivity extends Activity {

    DragGridView gridView;
    List<MenuBean> menuBeans;

    BaseAdapter adapter = new MyListAdapter();
    Gson gson = new Gson();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gridview);
        gridView = (DragGridView) findViewById(R.id.demo_grid_view);

        String infos = getString("menu_key");
        if (TextUtils.isEmpty(infos)) {
            menuBeans = getData();
        } else {
            menuBeans = gson.fromJson(infos, new TypeToken<List<MenuBean>>() {}.getType()); 
        }

        gridView.setAdapter(adapter);
        gridView.setOnItemChanageListener(new DragGridView.OnItemChanageListener() {
            @Override
            public void onChange(int from, int to) {
                if (from < to) {
                    for (int i = from; i < to; i++) {
                        Collections.swap(menuBeans, i, i + 1);
                    }
                } else if (from > to) {
                    for (int i = from; i > to; i--) {
                        Collections.swap(menuBeans, i, i - 1);
                    }
                }

                String jsonStr = gson.toJson(menuBeans); //将List转换成Json
                saveString("menu_key", jsonStr);

                adapter.notifyDataSetChanged();
            }
        });
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(DragActivity.this, "点击了:" + menuBeans.get(i).getTvTranslate(), Toast.LENGTH_LONG).show();
            }
        });
    }

    class MyListAdapter extends BaseAdapter {

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

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

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.app_item, null);
                viewHolder = new ViewHolder();
                viewHolder.title = (TextView) convertView.findViewById(R.id.tv_translate);
                viewHolder.tvDescribe = (TextView) convertView.findViewById(R.id.tv_describe);
                viewHolder.rlTextTrans = (RelativeLayout) convertView.findViewById(R.id.rl_text_trans);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.title.setText(menuBeans.get(position).getTvTranslate());
            viewHolder.tvDescribe.setText(menuBeans.get(position).getTvDescribe());
            viewHolder.rlTextTrans.setBackgroundResource(getResourceId(DragActivity.this,menuBeans.get(position).getImgResource()));

            return convertView;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }
        
        class ViewHolder {
            public TextView title;
            public TextView tvDescribe;

            public RelativeLayout rlTextTrans;
        }
    }


    private List<MenuBean> getData() {
        String[] transType = {"测试1", "测试2", "测试3"};
        String[] iconName = {"icon_texttran_bg", "icon_phototran_bg", "icon_voicetran_bg"};
        String[] describ = {"xxxx", "xxxxx", "xxxx"};
        List<MenuBean> menuBeans = new ArrayList<>();

        for (int i = 0; i < transType.length; i++) {
            MenuBean menuBean = new MenuBean();
            menuBean.setTvTranslate(transType[i]);
            menuBean.setImgResource(iconName[i]);
            menuBean.setTvDescribe(describ[i]);
            menuBeans.add(menuBean);
        }
        return menuBeans;
    }


    public final String SHARE_PREFERENCES_NAME = "PhotoTran";


    public String getString(String key) {
        return getSharedPreferences(SHARE_PREFERENCES_NAME, MODE_PRIVATE).getString(key, "");
    }

    public void saveString(String key, String value) {
        SharedPreferences sharedPreferences = getSharedPreferences(SHARE_PREFERENCES_NAME, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();
    }
    /**
     * 根据文件名称获取资源id
     * @param fileName
     * @return
     */
    public  int getResourceId(Context context, String fileName) {
        try {
            int id = context.getResources().getIdentifier(fileName, "mipmap", context.getPackageName());
            return id;
        } catch (Exception e) {

        }
        return 0;
    }
}

xml文件:

app_item布局

<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:background="#00000000"
    android:paddingLeft="18dp"
    android:paddingRight="18dp">

    <RelativeLayout
        android:id="@+id/rl_text_trans"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="18dp"
        android:background="@mipmap/icon_texttran_bg"
        android:paddingLeft="25dp">

        <TextView
            android:id="@+id/tv_translate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="45dp"
            android:text="@string/text_title"
            android:textColor="@color/blue_dark"
            android:textSize="26dp"
            android:textStyle="bold" />


        <TextView
            android:id="@+id/tv_describe"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv_translate"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="15dp"
            android:lineSpacingExtra="10dp"
            android:text="@string/text_content"
            android:textColor="@color/blue_light_dark"
            android:textSize="16dp" />
    </RelativeLayout>

</LinearLayout>

activity_gradview.xml:

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

    <com.demo.draggridview.DragGridView
        android:id="@+id/demo_grid_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numColumns="1" />

</LinearLayout>

WeChat_20220709162552

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_41620230

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值