私のアプリケーションには、TextView を保持する NestedScrollView があります。NestedScrollView の下に A RecyclerView を追加したいのですが、何も表示されません。ここに私のレイアウトxmlファイルがあります
activity_main.xml
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.trueblueoperator.samplescrolling.ScrollingActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="192dp"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:layout_width="match_parent"
android:layout_height="192dp"
android:scaleType="centerCrop"
android:src="@drawable/image"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_scrolling"/>
<include layout="@layout/recyclerview_content"/>
</android.support.design.widget.CoordinatorLayout>
content_scrolling.xml
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.trueblueoperator.swachhapp.StoryDetailFragment"
tools:showIn="@layout/fragment_story_detail">
<TextView
android:id="@+id/story_detail_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="@string/large_text" />
</android.support.v4.widget.NestedScrollView>
recyclerview_content.xml
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
これは、StaggeredviewLayout マネージャーを使用して Recyclerview を保持する私の Fragment です。
public class FragmentAllStory extends Fragment implements RecyclerView.OnScrollChangeListener{
//Creating a List of superheroes
private List<AllComments> allcommentList;
private RequestQueue requestQueue;
private Boolean orientation = true;
private ProgressBar progressBar;
//The request counter to send ?page=1, ?page=2 requests
private int requestCount = 1;
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
requestCount = 1;
View v = inflater.inflate(R.layout.recyclerview_content, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, 1));
}
else {
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, 1));
}
allcommentList = new ArrayList<>();
requestQueue = Volley.newRequestQueue(getContext());
fetchData();//data is fetched and stored in the ArrayList
recyclerView.setOnScrollChangeListener(this);
adapter = new AllCommentsAdapter(allcommentList, getActivity());
recyclerView.setAdapter(adapter);
return v;
}
private JsonArrayRequest getDataFromServer(int requestCount) {
//JsonArrayRequest of volley
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Config.DATA_URL + String.valueOf(requestCount),
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//Calling method parseData to parse the json response
parseData(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//If an error occurs that means end of the list has reached
Toast.makeText(getContext(), "No More Items Available", Toast.LENGTH_SHORT).show();
}
});
//Returning the request
return jsonArrayRequest;
}
//This method will get data from the web api
private void fetchData() {
//Adding the method to the queue by calling the method getDataFromServer
requestQueue.add(getDataFromServer(requestCount));
//Incrementing the request counter
requestCount++;
}
//This method will parse json data
private void parseData(JSONArray array) {
for (int i = 0; i < array.length(); i++) {
//Creating the superhero object
Allcomments allcomment = new Allcomments();
JSONObject json = null;
try {
//Getting json
json = array.getJSONObject(i);
//Adding data to the superhero object
allcomment.setImageUrl(json.getString(Config.TAG_IMAGE_URL));
allcomment.setName(json.getString(Config.TAG_NAME));
allcomment.setPublisher(json.getString(Config.TAG_PUBLISHER));
} catch (JSONException e) {
e.printStackTrace();
}
allcommentList.add(allcomment);
}
//Notifying the adapter that data has been added or changed
adapter.notifyDataSetChanged();
}
//This method would check that the recyclerview scroll has reached the bottom or not
private boolean isLastItemDisplaying(RecyclerView recyclerView) {
int[] info = new int[3];
if (recyclerView.getAdapter().getItemCount() != 0) {
((StaggeredGridLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPositions(info);
Log.d("info", "" + info[0] + " " + info[1]);
if (info[0] == recyclerView.getAdapter().getItemCount() - 1 || info[1] == recyclerView.getAdapter().getItemCount() - 1 || info[2] == recyclerView.getAdapter().getItemCount() - 1)
return true;
}
return false;
}
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
//Ifscrolled at last then
if (isLastItemDisplaying(recyclerView)) {
//Calling the method getdata again
fetchData();
}
}
}
御時間ありがとうございます。