如何在RecyclerView中添加第二个布局?

时间:2015-09-25 08:06:35

标签: android android-layout android-recyclerview

我的RecyclerView出了点问题。我想添加一个泡泡小部件,用于创建带消息的新圆圈(就像在messanger中一样)。问题是泡沫必须放在单独的XML布局文件中,所以我不能将它添加到"项目#34;我在适配器中的布局。在泡泡布局中我只有TextView,但是我不想显示普通文本 - 这太简单了;)我想显示我从API获得的数据。换句话说 - 我需要访问我的适配器中的Bubble布局内的TextView。请帮忙! :)

这是我的适配器类

package pl.krakowskascenamuzyczna.ksmcalendar.Adapter;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Date;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.Days;
import org.w3c.dom.Text;


import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.txusballesteros.bubbles.BubbleLayout;
import com.txusballesteros.bubbles.BubblesManager;

import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import pl.krakowskascenamuzyczna.ksmcalendar.Activities.CalendarActivity;
import pl.krakowskascenamuzyczna.ksmcalendar.Activities.DisplayConcertActivity;
import pl.krakowskascenamuzyczna.ksmcalendar.MySingleton;
import pl.krakowskascenamuzyczna.ksmcalendar.R;
import pl.krakowskascenamuzyczna.ksmcalendar.data.Concert;
import pl.krakowskascenamuzyczna.ksmcalendar.data.Thumbnail;


public class ConcertAdapter extends RecyclerView.Adapter<ConcertAdapter.MyViewHolder> {


    private LayoutInflater inflater;
    private Context context;
    NetworkImageView mNetworkImageView;
    private MySingleton mySingleton;
    private List<Concert> concertList = new ArrayList();


    public ConcertAdapter(Context context, List<Concert> concerts) {
        this.inflater = LayoutInflater.from(context);
        this.concertList = concerts;
        this.context = context;

    }

    public void setListConcert(ArrayList<Concert> concertList) {
        this.concertList = concertList;
        notifyItemRangeChanged(0, concertList.size());
    }


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

        View view = inflater.inflate(R.layout.concert_item, parent, false);
        MyViewHolder holder = new MyViewHolder(view);

        return holder;

    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {

        final float screenWidthPx = holder.itemView.getResources().getDisplayMetrics().widthPixels;

        Concert current = concertList.get(position);
        Log.d("mLog", current.getUrl());
        holder.image.setImageUrl(current.getUrl(), MySingleton.getInstance().getImageLoader());
        holder.image.getLayoutParams().height = (int) (screenWidthPx * 0.75);

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date concertDate = new Date();
        try {
            concertDate = format.parse(current.getDate());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        DateTime dt = new DateTime();
        DateTime currentDate = dt.withZone(DateTimeZone.forID("Europe/Warsaw"));
        int days = Days.daysBetween(new DateTime(currentDate), new DateTime(concertDate)).getDays();
        holder.date_btn.setText(String.valueOf(days));

    }

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

    public void  showDisplay(int position){
        Intent intent = new Intent(context, DisplayConcertActivity.class);
        intent.putExtra("position", position);
        intent.putExtra("content", concertList.get(position).getContent());
        intent.putExtra("date", concertList.get(position).getDate());
        intent.putExtra("url", concertList.get(position).getUrl());
        context.startActivity(intent);
    }


    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener  {

        private NetworkImageView image;
        private Button date_btn;

        public MyViewHolder(View itemView) {
            super(itemView);
            image = (NetworkImageView) itemView.findViewById(R.id.concerts_niv);
            date_btn = (Button) itemView.findViewById(R.id.date_btn);
            image.setOnClickListener(this);


        }
        @Override
        public void onClick(View v) {showDisplay(getAdapterPosition());

        }
    }


}

这是我的泡泡布局:

    <com.txusballesteros.bubbles.BubbleLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/bubble_tv"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_gravity="center"
        android:background="@drawable/profile_decorator"
        android:scaleType="centerCrop"
        android:src="@drawable/profile_decorator"
        android:textColor="#fff"
        />
</com.txusballesteros.bubbles.BubbleLayout>

1 个答案:

答案 0 :(得分:0)

您可以将气泡TextView作为参数传递给适配器构造函数。

private TextView mBubbleTextView;

public ConcertAdapter(Context context, List<Concert> concerts, TextView bubbleTextView) {
        this.inflater = LayoutInflater.from(context);
        this.concertList = concerts;
        this.context = context;
        this.mBubbleTextView = bubbleTextView;

}

现在您的适配器中有气泡TextView,因此可以从任何方法更新它。