对recycleView的页面进行点击跳转设计
一.消息界面跳转聊天界面
1.建立MainActivity2以及其对应的layout文件实现跳转
如图所示:
其中xml文件对应的textview应该为“朋友”(为了跳转之后的界面便于理解)
JAVA文件所对应的代码如下:
package com.example.mywork;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
public class MainActivity2 extends AppCompatActivity {
private TextView textView21;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE); //取消默认title
setContentView(R.layout.activity_main2);
Intent intent=getIntent();
textView21=findViewById(R.id.textView21);
textView21.setText(intent.getStringExtra("num"));
Log.d("life","activity2 is onCreate...");
}
@Override
protected void onStart() {
super.onStart();
Log.d("life", "activity2 is onStart...");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d("life", "activity2 is onRestart...");
}
@Override
protected void onPostResume() {
super.onPostResume();
Log.d("life", "activity2 is onPostResume...");
}
@Override
protected void onPause() {
super.onPause();
Log.d("life", "activity2 is onPuse...");
}
@Override
protected void onStop() {
super.onStop();
Log.d("life", "activity2 is onStop...");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("life", "activity2 is onDestroy...");
}
}
2.建立RecyItemTouchHelperCallback.java文件实现跳转
代码如下:
package com.example.mywork;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.ItemTouchHelper;
import java.util.Collections;
public class RecyItemTouchHelperCallback extends ItemTouchHelper.Callback {
RecyclerView.Adapter mAdapter;
boolean isSwipeEnable;
boolean isFirstDragUnable;
public RecyItemTouchHelperCallback(RecyclerView.Adapter adapter) {
mAdapter = adapter;
isSwipeEnable = true;
isFirstDragUnable = false;
}
public RecyItemTouchHelperCallback(RecyclerView.Adapter adapter, boolean isSwipeEnable, boolean isFirstDragUnable) {
mAdapter = adapter;
this.isSwipeEnable = isSwipeEnable;
this.isFirstDragUnable = isFirstDragUnable;
}
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN |
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
int swipeFlags = 0;
return makeMovementFlags(dragFlags, swipeFlags);
} else {
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
return makeMovementFlags(dragFlags, swipeFlags);
}
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
int fromPosition = viewHolder.getAdapterPosition();
int toPosition = target.getAdapterPosition();
if (isFirstDragUnable && toPosition == 0) {
return false;
}
if (fromPosition < toPosition) {
for (int i = fromPosition; i < toPosition; i++) {
Collections.swap(((MyAdapter2) mAdapter).getDataList(), i, i + 1);
}
} else {
for (int i = fromPosition; i > toPosition; i--) {
Collections.swap(((MyAdapter2) mAdapter).getDataList(), i, i - 1);
}
}
mAdapter.notifyItemMoved(fromPosition, toPosition);
return true;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
int adapterPosition = viewHolder.getAdapterPosition();
mAdapter.notifyItemRemoved(adapterPosition);
((MyAdapter2) mAdapter).getDataList().remove(adapterPosition);
}
@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
viewHolder.itemView.setBackgroundColor(Color.LTGRAY);
}
super.onSelectedChanged(viewHolder, actionState);
}
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
viewHolder.itemView.setBackgroundColor(Color.WHITE);
}
@Override
public boolean isLongPressDragEnabled() {
return !isFirstDragUnable;
}
@Override
public boolean isItemViewSwipeEnabled() {
return isSwipeEnable;
}
}
同时,我是建立了两个适配器adapter来实现跳转的,如图:
3.将之前的message的JAVA文件按与xml文件进行布局修改
xml文件如图:
JAVA文件代码如下:
package com.example.mywork;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class messageFragment extends Fragment {
private RecyclerView recyclerView2;
private Context context;
private MyAdapter2 myAdapter2;
public messageFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
// 压缩器,把xml文件压缩到容器中
View view=inflater.inflate(R.layout.fragment_message, container, false);
context=this.getActivity();
recyclerView2=view.findViewById(R.id.recyclerview2);
String[] user = new String[20];
String[] info = new String[20];
for (int i=0;i<20;i++){
user[i]=("朋友"+i);
info[i]=("这是第"+i+"条消息");
}
List<Map<String,Object>> items2=new ArrayList<Map<String,Object>>();
for(int i=0;i<user.length;i++) {
Map<String,Object> item2=new HashMap<String,Object>();
item2.put("USER", user[i]);
item2.put("MESSAGE",info[i]);
item2.put("png", R.drawable.user);
items2.add(item2);
}
myAdapter2=new MyAdapter2(items2,context);
recyclerView2.setAdapter(myAdapter2);
LinearLayoutManager layoutmanager2 = new LinearLayoutManager(this.getActivity());
layoutmanager2.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView2.setLayoutManager(layoutmanager2);
myAdapter2.setOnItemClickListener(new MyAdapter2.OnItemClickListener(){
@Override
public void onItemClick(View view , int position){
Intent intent=new Intent(context,MainActivity2.class);
intent.putExtra("num",user[position]);
startActivityForResult(intent,1);
}
});
RecyItemTouchHelperCallback itemTouchHelperCallback = new RecyItemTouchHelperCallback(myAdapter2);
final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemTouchHelperCallback);
itemTouchHelper.attachToRecyclerView(recyclerView2);
// 添加分割线
recyclerView2.addItemDecoration(new DividerItemDecoration(this.getActivity(), DividerItemDecoration.VERTICAL));
return view;
}
}
最后的结果如图: