我有一个有两个页面的ViewPager,每个页面都包含一个相同Fragment的实例。在每个片段中,我有一个相同的RecyclerView实例。问题是,在第一个RecyclerView.Adapter的项目点击事件中,它从第二个RecyclerView获取数据。 RecyclerView.Adapter的ViewHolder是:
public class ViewHolder extends RecyclerView.ViewHolder {
TextView name;
TextView timestamp;
public ViewHolder(final View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
timestamp = (TextView) itemView.findViewById(R.id.timestamp);
// Setup the click listener
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Triggers click upwards to the adapter on click
if (listener != null)
listener.onItemClick(itemView, getLayoutPosition());
}
});
}
在片段中:
listAdapter.setOnItemClickListener(new FeedListAdapter.OnItemClickListener() {
@Override
public void onItemClick(View itemView, int position) {
//feedItems.get(position) always returns the item of the second RecyclerView
//feedItems is datasource of the adapter
}
});
片段代码:
public class FeedListFragment extends Fragment {
private static final String TAG = FeedListFragment.class.getSimpleName();
private RecyclerView listView;
private FeedListAdapter listAdapter;
private List<Feed> feedItems = new ArrayList<Feed>();
public FeedListFragment(){
super();
}
private String getFeedUrl() {
Bundle bundle = getArguments();
if (bundle == null) {
return null;
}
return bundle.getString("feedUrl");
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.feed_list,container,false);
listView = (RecyclerView) v.findViewById(R.id.list);
listAdapter = new FeedListAdapter(getActivity(), feedItems);
listAdapter.setOnItemClickListener(new FeedListAdapter.OnItemClickListener() {
@Override
public void onItemClick(View itemView, int position) {
Intent intent = new Intent(getActivity(), FeedDetailActivity.class);
Bundle bundle = new Bundle();
bundle.putLong("feedId", feedItems.get(position).getId());
intent.putExtras(bundle);
intent.putExtra("feed", feedItems.get(position));
startActivity(intent);
}
});
listView.setAdapter(listAdapter);
listView.setLayoutManager(new LinearLayoutManager(getActivity()));
(new Handler()).postDelayed(
new Runnable() {
@Override
public void run() {
refreshContent();
}
},
1000
);
return v;
}
public void refreshContent(){
JsonObjectRequest jsonReq = new JsonObjectRequest(Request.Method.GET, getFeedUrl(), null, new Response.Listener<JSONObject>()
{
@Override
public void onResponse(final JSONObject response) {
if (response != null) {
//Need to process response with an AsyncTask or progressbar will be frozen.
// See http://stackoverflow.com/questions/27978274/volley-progressdialog-stuck-freeze-at-the-time-of-get-huge-amount-of-data
new AsyncTask<JSONObject, Void, List<Feed>>() {
@Override
protected List<Feed> doInBackground(JSONObject... params) {
ServerResponseStatus responseStatus = JSONUtil.parseServerResponseStatus(params[0]);
if (AppController.STATUS_OK.equals(responseStatus.getStatus())) {
return parseJsonFeed(params[0]);
} else {
switchState(State.ERROR, responseStatus.getErrorMessage());
}
return null;
}
@Override
protected void onPostExecute(List<Feed> feeds) {
if (feeds == null) {
return;
}
feedItems.clear();
feedItems.addAll(feeds);
listAdapter.notifyDataSetChanged();
switchState(State.READY, null);
}
}.execute(response);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//show error message
}
});
// Adding request to volley request queue
jsonReq.setShouldCache(false);
AppController.getInstance().addToRequestQueue(jsonReq);
}
/**
* Parsing json reponse and passing the data to feed view list adapter
* */
private List<Feed> parseJsonFeed(JSONObject response) {
List<Feed> feeds = null;
try {
JSONArray feedArray = response.getJSONArray("feeds");
feeds = JSONUtil.parseFeedList(feedArray, new JSONUtil.JSONParseOptions());
} catch (JSONException e) {
switchState(State.ERROR, e.toString());
return null;
}
return feeds;
}
}
ViewPagerAdapter代码:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
CharSequence titles[]; // This will Store the titles of the Tabs which are Going to be passed when ViewPagerAdapter is created
int numbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created
private FeedListFragment homeTab;
private FeedListFragment favTab;
// Build a Constructor and assign the passed Values to appropriate values in the class
public ViewPagerAdapter(FragmentManager fm, CharSequence mTitles[], int mNumbOfTabsumb) {
super(fm);
this.titles = mTitles;
this.numbOfTabs = mNumbOfTabsumb;
}
//This method return the fragment for the every position in the View Pager
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return getHomeTab();
case 1:
return getFavTab();
default:
return null;
}
}
// This method return the titles for the Tabs in the Tab Strip
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
// This method return the Number of tabs for the tabs Strip
@Override
public int getCount() {
return numbOfTabs;
}
public FeedListFragment getHomeTab() {
if (homeTab == null) {
Bundle bundle = new Bundle();
bundle.putString("feedUrl", "...");
homeTab = new FeedListFragment();
homeTab.setArguments(bundle);
}
return homeTab;
}
public FeedListFragment getFavTab() {
if (favTab == null) {
Bundle bundle = new Bundle();
bundle.putString("feedUrl", "...");
favTab = new FeedListFragment();
favTab.setArguments(bundle);
}
return favTab;
}
}
很奇怪,因为适配器中的所有变量都是非静态的。有没有人遇到这个问题,可以提供帮助? 感谢!!!