一对一视频源码,安卓实现grid可编辑导航栏
一对一视频源码,安卓实现grid可编辑导航栏实现的相关代码
GridLayout
```cpp package com.coral3.common_module.components; import android.content.Context; import android.content.DialogInterface; import android.content.SharedPreferences; import android.text.TextUtils; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.AdapterView; import android.widget.EditText; import android.widget.GridView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; import com.coral3.common_module.R; import com.coral3.common_module.adapter.GridAdapter; public class GridLayout extends LinearLayout { private Context mContext; private GridView gridView; private SharedPreferences sharedPreferences; public SharedPreferences.Editor editor; private String[] names = new String[]{ "lys", "yls", "syl", "sly", "ysl", "lsy" }; private int[] icons = new int[]{ R.mipmap.weixin, R.mipmap.weixin, R.mipmap.weixin, R.mipmap.weixin, R.mipmap.weixin, R.mipmap.weixin }; public GridLayout(Context context, @Nullable AttributeSet attrs) { super(context, attrs); mContext = context; View view = LayoutInflater.from(context).inflate(R.layout.layout_grid_view, this); initView(); } public void initView(){ sharedPreferences = mContext.getSharedPreferences("pos1", Context.MODE_PRIVATE); editor = sharedPreferences.edit(); gridView = findViewById(R.id.gv_grid_view); GridAdapter gridAdapter = new GridAdapter(mContext, R.layout.layout_grid_item, icons, names, sharedPreferences); gridView.setAdapter(gridAdapter); gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) { EditText editText = new EditText(mContext); TextView tv = view.findViewById(R.id.tv_grid_name); editText.setText(tv.getText().toString()); new AlertDialog.Builder(mContext) .setTitle("修改grid项目") .setMessage("修改名称") .setCancelable(false) .setView(editText) .setPositiveButton("ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { String v = editText.getText().toString(); if(!TextUtils.isEmpty(v)){ editor.putString("pos1", v).apply(); tv.setText(editText.getText().toString()); Toast.makeText(mContext, "设置成功", Toast.LENGTH_SHORT).show(); } } }) .setNegativeButton("cancel", null) .show(); return true; } }); } } ```
GridAdapter
```cpp package com.coral3.common_module.adapter; import android.content.Context; import android.content.SharedPreferences; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.coral3.common_module.R; public class GridAdapter extends BaseAdapter { private Context context; private int resource; private int[] icons; private String[] names; private SharedPreferences sp; public GridAdapter(Context context, int resource, int[] icons, String[] names, SharedPreferences sp){ this.context = context; this.resource = resource; this.icons = icons; this.names = names; this.sp = sp; } @Override public int getCount() { return names.length; } @Override public Object getItem(int i) { return names[i]; } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { viewHolder viewHolder; if(view == null){ view = LayoutInflater.from(context).inflate(resource, viewGroup, false); viewHolder = new viewHolder(); viewHolder.iv = view.findViewById(R.id.iv_grid_image); viewHolder.tv = view.findViewById(R.id.tv_grid_name); view.setTag(viewHolder); }else{ viewHolder = (viewHolder)view.getTag(); } viewHolder.iv.setImageResource(icons[i]); viewHolder.tv.setText(names[i]); if(i == 0){ String pos1 = sp.getString("pos1", null); if(pos1 != null){ viewHolder.tv.setText(pos1); } } return view; } class viewHolder{ ImageView iv; TextView tv; } } ```
layout_grid_view.xml
```cpp <?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"> <GridView android:id="@+id/gv_grid_view" android:layout_width="match_parent" android:verticalSpacing="15dp" android:numColumns="3" android:layout_marginTop="10dp" android:layout_height="wrap_content"/> </LinearLayout> ```
layout_grid_item.xml
```cpp <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <ImageView android:id="@+id/iv_grid_image" android:src="@mipmap/weixin" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:id="@+id/tv_grid_name" android:layout_width="match_parent" android:gravity="center" android:text="例子" android:layout_height="wrap_content"/> </LinearLayout> ```
使用
```cpp <com.coral3.common_module.components.GridLayout android:id="@+id/gl_grid_nav" android:layout_width="match_parent" android:layout_height="wrap_content"/> ```
以上就是一对一视频源码,安卓实现grid可编辑导航栏实现的相关代码, 更多内容欢迎关注之后的文章