gpt4 book ai didi

java - 更新时 Recyclerview 移至底部

转载 作者:行者123 更新时间:2023-12-01 16:51:25 25 4
gpt4 key购买 nike

您好,我正在创建一个聊天应用程序。应用程序运行良好。但是,当收到新消息(通过后台服务)时,我会更新 recyclerview,然后用户必须向下滚动才能看到新消息。

我想做的是,每当收到新消息时,我想更新回收器 View 并滚动到底部。这样用户无需滚动即可看到最后收到的消息。

聊天 Activity

public class Chatting extends AppCompatActivity {


private DrawerLayout drawerLayout;
private ApplicationEnvironmentURL applicationEnvironment;
private Toolbar toolbar;
private SelectVisitorService mService = new SelectVisitorService();
private boolean mBound = false;
public static Context baseContext;

private static String uniqueID;
public static ChattingAdapter mAdapter ;
public static List<ChattingItomObject> messageItems = new ArrayList<ChattingItomObject>();
private RecyclerView recyclerView;

public String ProfileId;
public String profileToken;
public String CompanyID;
public String DisplayName;

public String visitor_id;
public String visitor_name;

private EditText chat_message;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chatting);

SharedPreferences prefs = getSharedPreferences("zupportdesk", MODE_PRIVATE);
ProfileId = prefs.getString("ProfileId", "Not defined");
profileToken = prefs.getString("profileToken", "Not defined");
CompanyID = prefs.getString("companyId", "Not defined");
DisplayName = prefs.getString("DisplayName", "Not defined");

chat_message = (EditText) findViewById(R.id.ET_chating_message);
baseContext = getBaseContext();

Intent intent = getIntent();
visitor_id = intent.getStringExtra("visitor_id");
visitor_name = intent.getStringExtra("visitor_name");

toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
initNavigationDrawer();
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle(visitor_name);

uniqueID = UUID.randomUUID().toString();
Log.d("GUIID", uniqueID);

// add some items to list to test
messageItems.add(new ChattingItomObject("VisitorID", visitor_name+" has joined the chat!", "", "Other", "UserName", "Other", uniqueID));
messageItems.add(new ChattingItomObject("VisitorID", "Hi, how can I help you today?", "", "Other", "UserName", "Other", uniqueID));
messageItems.add(new ChattingItomObject("VisitorID", DisplayName+" has joined the chat!", "", "Other", "UserName", "Other", uniqueID));

recyclerView = (RecyclerView) findViewById(R.id.recycler_chat);
mAdapter = new ChattingAdapter(this, messageItems);

LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
}


public static void updateChatting(String visitor_id, String message, String date_time, String operator_type, String visitor_name){

Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(Chatting.baseContext, notification);
r.play();

uniqueID = UUID.randomUUID().toString();
Log.d("GUIID", uniqueID);

messageItems.add(new ChattingItomObject(visitor_id, message, date_time, operator_type, visitor_name, "Message_Type", uniqueID));

Handler refresh = new Handler(Looper.getMainLooper());
refresh.post(new Runnable() {
public void run(){mAdapter.notifyDataSetChanged();}
});

}

聊天适配器

public class ChattingAdapter extends RecyclerView.Adapter<ChattingViewHolders>{

public List<ChattingItomObject> ChattingItem;
private Context context;

public ChattingAdapter(Context context, List<ChattingItomObject> items){
this.ChattingItem = items;
this.context = context;
}

@Override
public ChattingViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {

Log.d("ChattingAdapter", String.valueOf(viewType));
if (viewType == 5001) {
// self message
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_chat_operator, null);
ChattingViewHolders rcv = new ChattingViewHolders(layoutView, context);
Log.d("ChattingAdapter", String.valueOf(viewType)+" - Operator");
return rcv;
} else if(viewType == 5002) {
// others message
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_chat_visitor, null);
ChattingViewHolders rcv = new ChattingViewHolders(layoutView, context);
Log.d("ChattingAdapter", String.valueOf(viewType)+" - visitor");
return rcv;
}else {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_chat_other, null);
ChattingViewHolders rcv = new ChattingViewHolders(layoutView, context);
Log.d("ChattingAdapter", String.valueOf(viewType)+" - Other");
return rcv;
}
}


@Override
public void onBindViewHolder(ChattingViewHolders holder, int position) {
ChattingItomObject operator = ChattingItem.get(position);
String operator_type = operator.getOperatorType().toString();
Log.d("ChattingAdapter", "Message_type : "+operator_type);

if(operator_type.equals("Operator")) {
holder.operatorMessage.setText(ChattingItem.get(position).getMessage());
holder.operatorTime.setText(ChattingItem.get(position).getDateTime());
} else if(operator_type.equals("Visitor")){
holder.visitorMessage.setText(ChattingItem.get(position).getMessage());
holder.visitortime.setText(ChattingItem.get(position).getDateTime());
} else {
holder.otherMessage.setText(ChattingItem.get(position).getMessage());
}
}


@Override
public int getItemViewType(int position) {
String message = ChattingItem.get(position).getOperatorType();
if (message.equals("Operator")) {
return 5001;
} else if(message.equals("Visitor")){
return 5002;
}

return position;
}

@Override
public int getItemCount() { return ChattingItem.size(); }
}

View 持有者

public class ChattingViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener{

public TextView operatorMessage;
public TextView operatorTime;
public TextView visitorMessage;
public TextView visitortime;
public TextView otherMessage;
private final Context context;

public ChattingViewHolders(View itemView, Context context) {
super(itemView);
this.context = context;
itemView.setOnClickListener(this);


operatorMessage = (TextView) itemView.findViewById(R.id.CCO_message);
operatorTime = (TextView) itemView.findViewById(R.id.CCo_time);
visitorMessage = (TextView) itemView.findViewById(R.id.CCV_message);
visitortime = (TextView) itemView.findViewById(R.id.CCV_time);
otherMessage = (TextView) itemView.findViewById(R.id.CC_other_message);
}

@Override
public void onClick(View view) {

}
}

Activity 聊天 XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
tools:context="zupportdesk.desk.zupport.chatsystem.Chatting">

<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.learn2crack.myapplication.MainActivity">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:id="@+id/user_details_app_bar"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_chat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:scrollbars="none"
android:layout_marginTop="55dp"
android:layout_marginBottom="45dp" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/colorPrimaryDark"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColorHint="#CFD8DC"
android:textColor="#CFD8DC"
android:hint="Write a message"
android:id="@+id/ET_chating_message"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/IV_Chatting_send"
android:layout_toStartOf="@+id/IV_Chatting_send"
android:layout_marginLeft="10dp" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5"
android:padding="4dp"
android:onClick="send_chat_message"
android:src="@android:drawable/ic_menu_send"
android:id="@+id/IV_Chatting_send"
android:layout_alignBottom="@+id/ET_chating_message"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>

</RelativeLayout>


</android.support.design.widget.CoordinatorLayout>


<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header"
app:menu="@menu/menu_navigation"/>
</android.support.v4.widget.DrawerLayout>

有人可以帮助我在 updateChatting 方法中更新时将 Recyclerview 滚动到底部吗?

最佳答案

notifyDataSetChanged() 之后调用此函数:

recyclerView.scrollToPosition(mAdapter.getItemCount()-1);

此外,当您只向 recyclerView 添加新项目时,您可以调用:

,而不是调用 notifyDataSetChanged()
mAdapter.notifyItemInserted(mAdapter.getItemCount()-1);

关于java - 更新时 Recyclerview 移至底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39443603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com