增加 点击变色、添加删除功能

master
wanghao 3 months ago
parent 08099f1e94
commit 9d82b28d85

@ -2,7 +2,9 @@ package com.example.readtext;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableBoolean;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
@ -37,6 +39,8 @@ public class MainActivity extends AppCompatActivity implements InPutDialog.InPut
private Intent intent;
private InPutDialog inPutDialog;
private ActivityMainBinding binding;
private ObservableBoolean state;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -44,8 +48,10 @@ public class MainActivity extends AppCompatActivity implements InPutDialog.InPut
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
intent = new Intent(this, ConfigActivity.class);
initAdapter();
inPutDialog=new InPutDialog(this);
state = new ObservableBoolean();
state.set(false);
binding.setState(state);
inPutDialog = new InPutDialog(this);
inPutDialog.setInPutDialogCall(this);
textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
@ -66,6 +72,7 @@ public class MainActivity extends AppCompatActivity implements InPutDialog.InPut
}
// 初始化加载器
private void initAdapter() {
adapter1 = new MainAdapter(this, new MyClickInterface() {
@ -74,9 +81,13 @@ public class MainActivity extends AppCompatActivity implements InPutDialog.InPut
textToSpeech.speak(list1.get(index).getHanzi(), TextToSpeech.QUEUE_FLUSH, null);
}
@SuppressLint("NotifyDataSetChanged")
@Override
public void onClickUpdateCall(int index) {
ReadTextBeen readTextBeen = list1.get(index);
readTextBeen.delete();
list1.remove(index);
adapter1.notifyDataSetChanged();
}
});
adapter2 = new MainAdapter(this, new MyClickInterface() {
@ -85,9 +96,13 @@ public class MainActivity extends AppCompatActivity implements InPutDialog.InPut
textToSpeech.speak(list2.get(index).getHanzi(), TextToSpeech.QUEUE_FLUSH, null);
}
@SuppressLint("NotifyDataSetChanged")
@Override
public void onClickUpdateCall(int index) {
ReadTextBeen readTextBeen = list2.get(index);
readTextBeen.delete();
list2.remove(index);
adapter2.notifyDataSetChanged();
}
});
adapter3 = new MainAdapter(this, new MyClickInterface() {
@ -96,9 +111,13 @@ public class MainActivity extends AppCompatActivity implements InPutDialog.InPut
textToSpeech.speak(list3.get(index).getHanzi(), TextToSpeech.QUEUE_FLUSH, null);
}
@SuppressLint("NotifyDataSetChanged")
@Override
public void onClickUpdateCall(int index) {
ReadTextBeen readTextBeen = list3.get(index);
readTextBeen.delete();
list3.remove(index);
adapter3.notifyDataSetChanged();
}
});
adapter4 = new MainAdapter(this, new MyClickInterface() {
@ -107,9 +126,13 @@ public class MainActivity extends AppCompatActivity implements InPutDialog.InPut
textToSpeech.speak(list4.get(index).getHanzi(), TextToSpeech.QUEUE_FLUSH, null);
}
@SuppressLint("NotifyDataSetChanged")
@Override
public void onClickUpdateCall(int index) {
ReadTextBeen readTextBeen = list4.get(index);
readTextBeen.delete();
list4.remove(index);
adapter4.notifyDataSetChanged();
}
});
binding.setAdapter1(adapter1);
@ -119,13 +142,14 @@ public class MainActivity extends AppCompatActivity implements InPutDialog.InPut
}
// 查询和加载列表
@SuppressLint("NotifyDataSetChanged")
@Override
protected void onResume() {
super.onResume();
list1 = LitePal.where("type =?","1").order("clickNum desc").find(ReadTextBeen.class);
list2 = LitePal.where("type =?","2").order("clickNum desc").find(ReadTextBeen.class);
list3 = LitePal.where("type =?","3").order("clickNum desc").find(ReadTextBeen.class);
list4 = LitePal.where("type =?","4").order("clickNum desc").find(ReadTextBeen.class);
list1 = LitePal.where("type =?", "1").order("clickNum desc").find(ReadTextBeen.class);
list2 = LitePal.where("type =?", "2").order("clickNum desc").find(ReadTextBeen.class);
list3 = LitePal.where("type =?", "3").order("clickNum desc").find(ReadTextBeen.class);
list4 = LitePal.where("type =?", "4").order("clickNum desc").find(ReadTextBeen.class);
adapter1.setList(list1);
adapter1.notifyDataSetChanged();
@ -135,24 +159,24 @@ public class MainActivity extends AppCompatActivity implements InPutDialog.InPut
adapter3.notifyDataSetChanged();
adapter4.setList(list4);
adapter4.notifyDataSetChanged();
// if (list == null || list.isEmpty()) {
// startActivity(intent);
// return;
// }
// adapter.setList(list);
// adapter.notifyDataSetChanged();
}
// 跳转到配置页面
// 设置
public void mainConfig(View view) {
// startActivity(intent);
boolean b = !state.get();
adapter1.setaBoolean(b);
adapter2.setaBoolean(b);
adapter3.setaBoolean(b);
adapter4.setaBoolean(b);
state.set(b);
}
public void mainAdd1(View view) {
inPutDialog.showAdd(1);
}
public void mainAdd2(View view) {
@ -176,29 +200,43 @@ public class MainActivity extends AppCompatActivity implements InPutDialog.InPut
super.onDestroy();
}
// 添加数据
@Override
public void inPutDialogReculi(String name, boolean b, int id, int type) {
ReadTextBeen readTextBeen=new ReadTextBeen();
ReadTextBeen readTextBeen = new ReadTextBeen();
readTextBeen.setHanzi(name);
String pinyinString ;
if (name.equals("睡觉")){
pinyinString="shui jiao";
}else {
pinyinString= PinyinHelper.convertToPinyinString("快乐音乐", " ", PinyinFormat.WITHOUT_TONE);
String pinyinString;
if (name.equals("睡觉")) {
pinyinString = "shui jiao";
} else {
pinyinString = PinyinHelper.convertToPinyinString(name, " ", PinyinFormat.WITHOUT_TONE);
}
readTextBeen.setPinyin(pinyinString);
readTextBeen.setType(type);
readTextBeen.setClickNum(0);
readTextBeen.save();
addBeen(type, readTextBeen);
inPutDialog.dismiss();
}
private void addBeen(int type){
private void addBeen(int type, ReadTextBeen readTextBeen) {
if (type == 1) {
list1.add(readTextBeen);
adapter1.setSelectedList(list1.size());
adapter1.notifyDataSetChanged();
} else if (type == 2) {
list2.add(readTextBeen);
adapter2.setSelectedList(list2.size());
adapter2.notifyDataSetChanged();
} else if (type == 3) {
list3.add(readTextBeen);
adapter3.setSelectedList(list3.size());
adapter3.notifyDataSetChanged();
} else if (type == 4) {
list4.add(readTextBeen);
adapter4.setSelectedList(list4.size());
adapter4.notifyDataSetChanged();
}
}
}

@ -1,6 +1,7 @@
package com.example.readtext.adapter;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
@ -22,6 +23,11 @@ public class MainAdapter extends RecyclerView.Adapter<MainAdapter.MyViewHolder>
private MyClickInterface clickInterface;
private LayoutInflater layoutInflater;
private List<Boolean> selectedList;
private boolean aBoolean=false;
public void setaBoolean(boolean aBoolean) {
this.aBoolean = aBoolean;
notifyDataSetChanged();
}
public MainAdapter(Context context, MyClickInterface clickInterface) {
this.context = context;
@ -32,6 +38,11 @@ public class MainAdapter extends RecyclerView.Adapter<MainAdapter.MyViewHolder>
public void setList(List<ReadTextBeen> list) {
this.list = list;
int size = list.size();
setSelectedList(size);
}
public void setSelectedList(int size) {
selectedList = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
selectedList.add(false);
@ -47,17 +58,42 @@ public class MainAdapter extends RecyclerView.Adapter<MainAdapter.MyViewHolder>
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
Log.e("TAG", "onBindViewHolder:运行"+position );
ReadTextBeen readTextBeen = list.get(position);
ItemMainBinding binding = holder.binding;
binding.setItem(readTextBeen);
binding.setState(aBoolean);
if (selectedList.get(position)) {
// 设置选中时的颜色,这里以设置背景颜色为例
binding.itemView.setBackgroundResource(R.drawable.item_main_bg2);
} else {
// 设置未选中时的颜色
binding.itemView.setBackgroundResource(R.drawable.item_bg);
}
binding.itemMainLinear.setOnClickListener(v -> {
clickInterface.onClickCall(position);
if (position != RecyclerView.NO_POSITION) {
// 切换选中状态
selectedList.set(position, !selectedList.get(position));
notifyItemChanged(position);
int size = selectedList.size();
for (int i = 0; i < size; i++) {
if (selectedList.get(i)){
selectedList.set(i,false);
}
}
selectedList.set(position,true);
notifyDataSetChanged();
/* if (position!= RecyclerView.NO_POSITION) {
// selectedList.set(position, !selectedList.get(position));
binding.itemView.setBackgroundResource(R.drawable.item_main_bg2);
notifyItemChanged(position);
if (oldTag==-1){
return;
}
}*/
});
binding.itemDelete.setOnClickListener(v -> clickInterface.onClickUpdateCall(position));
holder.binding.executePendingBindings();
}

@ -10,6 +10,8 @@ public class ReadTextBeen extends LitePalSupport {
private int clickNum;
private int type;
public int getId() {
return id;
}

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="5dp" />
<solid android:color="@color/bg1" />
</shape>

@ -21,7 +21,10 @@
<variable
name="adapter4"
type="com.example.readtext.adapter.MainAdapter" />
<variable
name="state"
type="androidx.databinding.ObservableBoolean" />
<import type="android.view.View"/>
</data>
<LinearLayout
@ -70,6 +73,7 @@
android:layout_marginRight="5dp"
android:layout_weight="1"
android:background="@color/white"
android:adapter="@{adapter1}"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
<androidx.recyclerview.widget.RecyclerView
@ -78,7 +82,7 @@
android:layout_marginRight="5dp"
android:layout_weight="1"
android:background="@color/white"
android:adapter="@{adapter2}"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
<androidx.recyclerview.widget.RecyclerView
@ -87,7 +91,7 @@
android:layout_marginRight="5dp"
android:layout_weight="1"
android:background="@color/white"
android:adapter="@{adapter3}"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
<androidx.recyclerview.widget.RecyclerView
@ -96,13 +100,14 @@
android:layout_marginRight="5dp"
android:layout_weight="1"
android:background="@color/white"
android:adapter="@{adapter4}"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="@{state?View.VISIBLE:View.GONE}"
android:layout_marginBottom="5dp">
<Button

@ -2,34 +2,58 @@
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="item"
type="com.example.readtext.been.ReadTextBeen" />
<variable
name="state"
type="Boolean" />
<import type="android.view.View"/>
</data>
<LinearLayout
<FrameLayout
android:id="@+id/item_main_linear"
android:layout_width="350dp"
android:layout_height="180dp"
android:background="@drawable/item_bg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1"
android:text="@{item.hanzi}"
android:textColor="#349756"
android:textSize="60sp" />
<TextView
<LinearLayout
android:id="@+id/item_view"
android:layout_width="match_parent"
android:layout_height="65dp"
android:gravity="center|top"
android:text="@{item.pinyin}"
android:textColor="#349756"
android:textSize="39sp" />
</LinearLayout>
android:layout_height="160dp"
android:background="@drawable/item_bg"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="@{item.hanzi}"
android:textColor="@color/black"
android:textSize="50sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="65dp"
android:gravity="center|top"
android:text="@{item.pinyin}"
android:textColor="@color/black"
android:textSize="29sp" />
</LinearLayout>
<ImageView
android:id="@+id/item_delete"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/delete"
android:layout_gravity="right"
android:visibility="@{state?View.VISIBLE:View.GONE}"/>
</FrameLayout>
</layout>
Loading…
Cancel
Save