2017-02-11 44 views
1

我有一個適配器鏈接到RecyclerView。我試圖實現聊天屏幕,我使用套接字來獲取消息,並且如果消息來自用戶的手機聊天氣泡顯示在屏幕的左側部分,如果消息來自對話者的手機聊天氣泡顯示在屏幕的左側部分。讓我們來看看:在recyclerview的適配器中設置邊距

普通視圖:

enter image description here

當我打開鍵盤輸入的東西后,不順心的事:

打開鍵盤:

enter image description here

但當我上下滾動RecyclerView時,一切都變好了。

這裏是我的代碼:

聊天屏幕布局:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:background="#F3F7FD"> 

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

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/recyclerView" 
      android:layout_marginBottom="47dp" 
      android:paddingTop="6dp" 
      android:paddingBottom="8dp" 
      android:clipToPadding="false" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

     <LinearLayout 
      android:id="@+id/element1" 
      android:layout_width="match_parent" 
      android:layout_height="47dp" 
      android:background="#FFFFFF" 
      android:orientation="horizontal" 
      android:layout_alignParentBottom="true"> 

      <ImageView 
       android:id="@+id/sAttach" 
       android:layout_width="30dp" 
       android:layout_height="30dp" 
       android:layout_weight="0" 
       android:layout_marginTop="9dp" 
       android:layout_marginLeft="11dp" 
       android:layout_marginRight="9dp" 
       android:src="@drawable/attach" 
       android:tint="#A7A7A7" 
       android:background="?selectableItemBackgroundBorderless" 
       android:clickable="true" /> 

      <ProgressBar 
       android:id="@+id/updateBar" 
       android:layout_width="30dp" 
       android:layout_height="30dp" 
       android:layout_weight="0" 
       android:layout_marginTop="9dp" 
       android:layout_marginLeft="11dp" 
       android:layout_marginRight="9dp" 
       android:indeterminate="true" 
       android:indeterminateTintMode="src_atop" 
       android:visibility="gone" /> 

      <EditText 
       android:id="@+id/msg" 
       android:layout_width="match_parent" 
       android:layout_height="47dp" 
       android:layout_weight="1" 
       android:paddingLeft="2dp" 
       android:textSize="16sp" 
       android:inputType="textCapSentences" 
       android:maxLines="1" 
       android:hint="@string/hint_message" 
       android:background="#FFFFFF"/> 

      <ImageView 
       android:id="@+id/sBt" 
       android:layout_width="33dp" 
       android:layout_height="33dp" 
       android:layout_weight="0" 
       android:paddingTop="8dp" 
       android:paddingBottom="8dp" 
       android:paddingLeft="9dp" 
       android:paddingRight="7dp" 
       android:layout_marginTop="7dp" 
       android:layout_marginLeft="15dp" 
       android:layout_marginRight="10dp" 
       android:src="@drawable/send" 
       android:background="@drawable/circle_send_gray" 
       android:clickable="true" /> 

     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/element2" 
      android:layout_width="match_parent" 
      android:layout_height="47dp" 
      android:background="#FFFFFF" 
      android:orientation="horizontal" 
      android:layout_alignParentBottom="true" 
      android:visibility="gone"> 

      <TextView 
       android:id="@+id/blockMessage" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:gravity="center"/> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="1px" 
      android:layout_marginBottom="47dp" 
      android:layout_alignParentBottom="true" 
      android:orientation="vertical" 
      android:background="#20000000" /> 

    </RelativeLayout> 

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

這裏是Chat.class:

private JsonArrayRequest getDataFromServer(String user, String room) { 

     //JsonArrayRequest of volley 
     JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(HERE GOES MY APIS URL, 
       new Response.Listener<JSONArray>() { 
        @Override 
        public void onResponse(JSONArray response) { 
         //Calling method parseData to parse the json response 
         parseData(response); 
         //Hiding the progressbar 

        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 

         //If an error occurs that means end of the list has reached 

        } 
       }); 

     //Returning the request 
     return jsonArrayRequest; 
    } 

    private void parseData(JSONArray array) { 
     for (int i = 0; i < array.length(); i++) { 
      //Creating the superhero object 
      ChatList superHero = new ChatList(); 
      JSONObject json = null; 
      try { 
       //Getting json 
       json = array.getJSONObject(i); 

       superHero.setMessage(json.getString("message")); 
       superHero.setFromtype(json.getString("type")); 
       superHero.setPhoto(json.getString("photo")); 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      //Adding the superhero object to the list 
      listSuperHeroes.add(0, superHero); 
     } 

     recyclerView.scrollToPosition(listSuperHeroes.size() -1); 

     //Notifying the adapter that data has been added or changed 
     adapter.notifyDataSetChanged(); 
    } 

聊天適配器:

public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder> { 

    private Context context; 

    private float scale; 

    private int dpAsPixels; 
    private int dpAsPixels2; 
    private int dpAsPixels3; 

    private LinearLayout.LayoutParams params; 
    private LinearLayout.LayoutParams params2; 

    //List to store all superheroes 
    static List<ChatList> superHeroes; 

    public ChatAdapter(List<ChatList> superHeroes, Context context){ 
     super(); 
     //Getting all superheroes 
     this.superHeroes = superHeroes; 
     this.context = context; 
    } 

    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_list, parent, false); 
     ChatAdapter.ViewHolder viewHolder = new ViewHolder(v); 

     scale = context.getResources().getDisplayMetrics().density; 
     dpAsPixels = (int) (3 * scale + 0.5f); 
     dpAsPixels2 = (int) (12 * scale + 0.5f); 
     dpAsPixels3 = (int) (12 * scale + 0.5f)+180; 

     params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 

     params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 

     return viewHolder; 
    } 

    @Override 
    public void onBindViewHolder(ChatAdapter.ViewHolder holder, int position) { 

     //Getting the particular item from the list 
     final ChatList superHero = superHeroes.get(position); 
     //Showing data on the views 

     if(superHero.getMessage().equals("image")) { 
      Picasso.with(context).load("http://mywebsite.com/uploads/media/"+superHero.getPhoto()).into(holder.photo); 
      holder.photo.setVisibility(ImageView.VISIBLE); 
      holder.message.setVisibility(TextView.GONE); 
      params2.bottomMargin = dpAsPixels+5; 
      params2.topMargin = dpAsPixels+5; 
      if(superHero.getFromtype().equals("he")) { 
       params.setMargins(dpAsPixels2, dpAsPixels, dpAsPixels3, dpAsPixels); 
       holder.photo.setLayoutParams(params2); 
       holder.photo.setBackgroundResource(R.drawable.chat_bubble_photo); 
       holder.textCard.setGravity(Gravity.LEFT); 
      }else{ 
       params.setMargins(dpAsPixels3, dpAsPixels, dpAsPixels2, dpAsPixels); 
       holder.photo.setLayoutParams(params2); 
       holder.photo.setBackgroundResource(R.drawable.chat_bubble_me_photo); 
       holder.textCard.setGravity(Gravity.RIGHT); 
      } 

      holder.textCard.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        ((Chat)context).openMedia(superHero.getPhoto()); 
       } 
      }); 
     }else{ 
      holder.photo.setVisibility(ImageView.GONE); 
      holder.message.setVisibility(TextView.VISIBLE); 
      holder.message.setText(decodeMessage(superHero.getMessage())); 
     } 

     if(superHero.getFromtype().equals("he")) { 
      params.setMargins(dpAsPixels2, dpAsPixels, dpAsPixels3, dpAsPixels); 
      holder.message.setLayoutParams(params); 
      holder.message.setBackgroundResource(R.drawable.chat_bubble); 
      holder.message.setTextColor(context.getResources().getColor(R.color.darkgray)); 
      holder.textCard.setGravity(Gravity.LEFT); 
     }else{ 
      params.setMargins(dpAsPixels3, dpAsPixels, dpAsPixels2, dpAsPixels); 
      holder.message.setLayoutParams(params); 
      holder.message.setBackgroundResource(R.drawable.chat_bubble_me); 
      holder.message.setTextColor(context.getResources().getColor(R.color.colorPrimary)); 
      holder.textCard.setGravity(Gravity.RIGHT); 
     } 

    } 

    public int getItemCount() { 
     return superHeroes.size(); 
    } 

    class ViewHolder extends RecyclerView.ViewHolder{ 
     //Views 
     public TextView message; 
     public LinearLayout textCard; 
     public ImageView photo; 

     //Initializing Views 
     public ViewHolder(View itemView) { 
      super(itemView); 
      message = (TextView) itemView.findViewById(R.id.message); 
      textCard = (LinearLayout) itemView.findViewById(R.id.textCard); 
      photo = (ImageView) itemView.findViewById(R.id.photo); 
     } 
    } 

    private String decodeMessage(String message) { 
     String raz = URLDecoder.decode(message); 
     return StringEscapeUtils.unescapeJava(raz); 
    } 

} 

回答

0

我已經解決了這個問題是這樣的:

我已經添加2個不同的聊天氣泡對傳入和傳出消息,並且在ChatAdapter調整他們通過setVisibility設定應顯示該氣泡。邊距設置在佈局文件中,而不是來自ChatAdapter。

0

對不起我不能無法評論這就是爲什麼在這裏給出的答案

當您打開鍵盤在那個時候,你必須在它的最後位置滾動recyclerview。

mLinearLayoutManager.scrollToPosition(yourList.size()); 

希望它有效。

+0

哦,我已經試過了,現在,它仍然沒有工作:( –

+0

你能幫助我在清單的Android您的活動標籤 –

+0

請試試這個:?windowSoftInputMode =「adjustResize」 –