我有一个具有如下数据的API:
{"seasons":[
{
"id":"1",
"titles":"1title1;1title2;1title3",
"url":"1url1;1url2;1url3;"
},
{
"id":"2",
"titles":"2title1;2title2;2title3",
"url":"2url1;2url2;2url3;"
}
]}
我很好地解析了它,在包裹类中,我这样做是为了标题:
public String getTitles() {
return titles;
}
public StringBuilder getFullTitles() {
StringBuilder builder = new StringBuilder();
String[] titlesArray = getTitles().split(";");
for (String title : titlesArray) {
builder.append(title + "\n");
}
这是网址:
public String getUrlAdaptive() {
return urladaptive;
}
public StringBuilder getFullUrlAdaptiveFinal() {
StringBuilder builder2 = new StringBuilder();
String[] urlArray = getUrlAdaptive().split(";");
for (String details : urlArray) {
builder2.append(details + "\n");
}
return builder2;
}
因此,在我的recycleview适配器中,我在textview中获取标题数据,如下所示:
@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
holder.title.setText(String.valueOf(mMoviesItems.get(position).getFullTitles()));
}
而我在recycleview应用程序中得到的结果是这样的:
1title1
1title2
1title3
2title1
2title2
2title3
现在您可以看到该组:
1title1
1title2
1title3
请参阅recycleview的第一项 和这个小组:
2title1
2title2
2title3
请参阅recycleview的第二项
您可以将其与api进行比较以了解我的意思
我需要做的是使每个组的每个项目都可以被点击
就像当我单击 1title1 时一样,我获得了与该标题对应的网址,即 1url1 在youtube中打开 当我点击 1title2 时,我得到了与该标题对应的网址,即 1url2 在youtube中打开 当我点击 2title2 时,我得到了与该标题对应的网址,即 2url2 可以在youtube中打开它
以此类推
就像在recycleview的每个主要项目中的点击项目一样
这是尝试答案后的代码:
@Override
public void onBindViewHolder(final RecyclerViewHolder holder, final int position) {
holder.title.setText(String.valueOf(mMoviesItems.get(position).getFullTitles()));
holder.title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View vview) {
// here when click on each title I need to get the getFullUrlAdaptiveFinal with it to play it
}
});
}
这是series_list_item.xml的完整代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#21293B"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="14sp"
android:visibility="visible" />
<TextView
android:id="@+id/url"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="14sp"
android:visibility="visible" />
</LinearLayout>
适配器代码:
public class SeriesAdapter extends RecyclerView.Adapter<SeriesAdapter.RecyclerViewHolder> {
ArrayList<SeriesItem> mMoviesItems;
private Context context;
private SeriesAdapterOnClickHandler mClickHandler;
public interface SeriesAdapterOnClickHandler {
void onClick(SeriesItem movie);
}
public SeriesAdapter(SeriesAdapterOnClickHandler clickHandler) {
mClickHandler = clickHandler;
}
public void setClickListener(SeriesAdapterOnClickHandler callback) {
mClickHandler = callback;
}
class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public final TextView SeriesSeasontitle;
public final TextView urlAdaptive;
public RecyclerViewHolder(View view) {
super(view);
SeriesSeasontitle = (TextView)itemView.findViewById(R.id.title);
urlAdaptive = (TextView)itemView.findViewById(R.id.url);
view.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int adapterPosition = getAdapterPosition();
SeriesItem movie = mMoviesItems.get(adapterPosition);
String text = "already Added To Favorites";
Toast.makeText(context, text, Toast.LENGTH_LONG).show();
mClickHandler.onClick(movie);
}
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
context = viewGroup.getContext();
int layoutIdForListItem = R.layout.series_list_item;
LayoutInflater inflater = LayoutInflater.from(context);
boolean shouldAttachToParentImmediately = false;
View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately);
RecyclerViewHolder holder = new RecyclerViewHolder(view);
return new RecyclerViewHolder(view);
}
@Override
public void onBindViewHolder(final RecyclerViewHolder holder, final int position) {
holder.SeriesSeasontitle.setText(String.valueOf(mMoviesItems.get(position).getFullTitles()));
holder.SeriesSeasontitle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View vview) {
String text = "text";
Toast.makeText(context, text, Toast.LENGTH_LONG).show();
}
});
}
@Override
public int getItemCount() {
if (null == mMoviesItems)
return 0;
else {
return mMoviesItems.size();
}
}
@Override
public int getItemViewType(int position) {
if (null == mMoviesItems)
return 0;
else {
return mMoviesItems.size();
}
}
public void setMovieData(ArrayList<SeriesItem> movieData) {
mMoviesItems = movieData;
notifyDataSetChanged();
}
}
活动代码:
SeriesDetail extends AppCompatActivity implements
SeriesAdapter.SeriesAdapterOnClickHandler {
SeriesAdapter mAdapterTrailer;
RecyclerViewAdapterOthers mAdapterOthers;
@BindView(R.id.rv_videos)
RecyclerView mVideosList;
@BindView(R.id.tv_error_message_display3)
TextView mErrorMessageDisplay3;
@BindView(R.id.rv_videos2)
RecyclerView mTrailersList;
@BindView(R.id.pb_loading_indicator_trailers2)
ProgressBar mLoadingIndicatorTrailers2;
String sortOrder2="seasons/1435";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_series_detail);
ButterKnife.bind(this);
LinearLayoutManager LayoutManager2 = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
mTrailersList.setLayoutManager(LayoutManager2);
mTrailersList.setHasFixedSize(true);
mAdapterTrailer = new SeriesAdapter(this);
mTrailersList.setAdapter(mAdapterTrailer);
loadVideosData(String.valueOf(sortOrder2));
}
private void loadVideosData(String movieId) {
showVideoDataView();
new FetchVideosTask().execute(movieId);
}
@Override
public void onClick(SeriesItem video) {
}
private void showTrailerDataView() {
mErrorMessageDisplay3.setVisibility(View.INVISIBLE);
mTrailersList.setVisibility(View.VISIBLE);
}
private void showErrorMessage3() {
mTrailersList.setVisibility(View.INVISIBLE);
mErrorMessageDisplay3.setVisibility(View.VISIBLE);
}
public class FetchVideosTask extends AsyncTask<String, Void, ArrayList<SeriesItem>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mLoadingIndicatorTrailers2.setVisibility(View.VISIBLE);
}
@Override
protected ArrayList<SeriesItem> doInBackground(String... params) {
if (params.length == 0) {
return null;
}
String movieId = params[0];
URL videosRequestUrl = NetworkSeries.buildUrl(movieId);
try {
String jsonVideoResponse = NetworkSeries.getResponseFromHttpUrl(videosRequestUrl);
ArrayList<SeriesItem> simpleJsonVideoData = JsonShashaDetailSeries.getSimpleMovieStringsFromJson(SeriesDetail.this, jsonVideoResponse);
return simpleJsonVideoData;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(ArrayList<SeriesItem> videoData) {
mLoadingIndicatorTrailers2.setVisibility(View.INVISIBLE);
if (videoData != null) {
showTrailerDataView();
mAdapterTrailer.setMovieData(videoData);
} else {
showErrorMessage3();
}
}
}
@Override
protected void onStart() {
super.onStart();
}
}
和可拆分类:
public class SeriesItem implements Parcelable {
private String titles;
private String urladaptive;
public SeriesItem(String titles, String urladaptive) {
this.titles = titles;
this.urladaptive = urladaptive;
}
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeString(titles);
out.writeString(urladaptive);
}
private SeriesItem(Parcel in) {
this.titles = in.readString();
this.urladaptive = in.readString();
}
public SeriesItem() {
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<SeriesItem> CREATOR = new Creator<SeriesItem>() {
@Override
public SeriesItem createFromParcel(Parcel in) {
return new SeriesItem(in);
}
@Override
public SeriesItem[] newArray(int i) {
return new SeriesItem[i];
}
};
public String getTitles() {
return titles;
}
public StringBuilder getFullTitles() {
StringBuilder builder = new StringBuilder();
String[] titlesArray = getTitles().split(";");
for (String details : titlesArray) {
builder.append(details + "\n");
}
return builder;
}
public String getUrlAdaptive() {
return urladaptive;
}
public StringBuilder getFullUrlAdaptiveFinal() {
StringBuilder builder2 = new StringBuilder();
String[] urlArray = getUrlAdaptive().split(";");
for (String details : urlArray) {
builder2.append(details + "\n");
}
return builder2;
}
}
当我单击上面的代码以测试项目的点击时,我只是尝试显示一条吐司消息,并且它可以工作 但是如果我想像现在这样单击标题时,则开始播放相同项目的网址 我如何处理此点击?在适配器本身还是活动中?
答案 0 :(得分:3)
是的,这是可能的。只需使用OnClickListener并将其设置在onBindViewHolder()中即可。
它看起来像这样:
@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
holder.title.setText(String.valueOf(mMoviesItems.get(position).getFullTitles()));
holder.title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// Toast here
}
}
}