2017-02-11 9 views
1

私はRecyclerViewにリンクされたアダプタを持っています。私はチャット画面を実装しようとしている、私はメッセージをフェッチするためにソケットを使用し、メッセージがユーザーの電話から来た場合は、画面の左側にバブルが表示され、メッセージが対話者の電話から来た場合、何かがうまくいかない、recyclerviewのアダプタのマージンを設定する

enter image description here

そして、私は何かを入力するキーボードを開いたときに後:

ノーマルビュー:見てみましょう

オープンキーボード:

enter image description here

しかし、 RecyclerViewを上下にスクロールすると、すべてが正常になります。ここで

私のコードです:

チャット画面レイアウト:

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(); 
    } 

チャットアダプタ::ここで

<?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がある

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

は申し訳ありませんが、私はここに

を答えを与える理由のthatsコメントすることができ傾くとき、あなたがその最後の位置にrecyclerviewをスクロールする必要があり、その時点で開いてキーボード。

mLinearLayoutManager.scrollToPosition(yourList.size()); 

希望します。

+0

ああ、私は –

+0

はあなたが私を助けることができる:(今、それはまだ動作しないことを試みた –

+0

マニフェストであなたのアクティビティタグでこれを試してみてくださいアンドロイド:?。windowSoftInputMode =「adjustResize」 –

関連する問題