请帮助纠正这个问题....我想在使用volly库i实现的单个活动中放置2-3个recycleview但是第二个recycleview没有显示
这些类包含2-3个recycleview和gridviewlayout ...其中我想显示具有特定文本的画廊 第一个画廊显示5-10个带有标题文字的图像,第二个画廊也将显示带有画廊标题文字的图像,与第三个画廊相同....但是这样做之后 第1次重新审核显示,第2次/第3次未显示
Gallery_byVolly.java
package com.example.gallery;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.example.admin.app_shewale.R;
import com.example.vikaskame.VikasGallery;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class Gallery_byVolly extends AppCompatActivity {
RecyclerView gallerygridview1;
TextView gallerytext;
List<VikasGallery> gallerylist1;
private RequestQueue requestQueue,requestQueue2,requestQueue3;
CardAdapter adapter,adapter2,adapter3;
private int requestCount = 1;
public static final String DATA_URL="http://1biz.in/shevale_app/android/shevale_gallery.php?page=";
public static final String DATA_UR1= "http://1biz.in/shevale_app/android/shevale_gallery.php?page=";
public static final String DATA_URL2= "http://1biz.in/shevale_app/android/shevale_gallery3.php?page=";
private RecyclerView.LayoutManager layoutManager,layoutManager2,layoutManager3;
Context context;
RecyclerView vollygallerygridview2;
List<VikasGallery> vollygallerylist2;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery_by_volly);
requestQueue = Volley.newRequestQueue(this);
gallerylist1 = new ArrayList<>();
gallerygridview1 = (RecyclerView) findViewById(R.id.vollygallerygridview);
gallerygridview1.setHasFixedSize(true);
layoutManager = new GridLayoutManager(this,2);
gallerygridview1.setLayoutManager(layoutManager);
getData();
adapter = new CardAdapter(gallerylist1, this);
//Adding adapter to recyclerview
gallerygridview1.setAdapter(adapter);
gallerygridview1.addOnItemTouchListener(
new Recylceviewitelclick(context, new Recylceviewitelclick.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
System.out.println("value of this is:" + position);
Toast.makeText(Gallery_byVolly.this, "value of this is" + position, Toast.LENGTH_SHORT).show();
Intent i = new Intent(getApplicationContext(), Second_Full_Gallery.class);
Bundle bundle = new Bundle();
bundle.putInt("vollygalleryposition", position);
bundle.putSerializable("listimages", (Serializable) gallerylist1);
i.putExtras(bundle);
System.out.println("value oofd getpostion" + position);
startActivity(i);
}
})
);
gallerygridview1.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (isLastItemDisplaying(gallerygridview1)) {
//Calling the method getdata again
getData();
}
}
});
vollygallerygridview2=(RecyclerView)findViewById(R.id.vollygallerygridview2);
vollygallerygridview2.setHasFixedSize(true);
vollygallerylist2=new ArrayList<>();
layoutManager2 = new GridLayoutManager(this,2);
vollygallerygridview2.setLayoutManager(layoutManager2);
getData2();
adapter2 = new CardAdapter(vollygallerylist2, this);
//Adding adapter to recyclerview
vollygallerygridview2.setAdapter(adapter2);
vollygallerygridview2.setOnScrollListener(new RecyclerView.OnScrollListener()
{
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState)
{
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (isLastItemDisplaying(vollygallerygridview2))
{
//Calling the method getdata again
getData2();
}
}
});
}
private void getData2()
{
requestQueue.add(getDataFromServer2(requestCount));
requestCount++;
}
private JsonArrayRequest getDataFromServer2(int requestCount)
{
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(DATA_URL2 + String.valueOf(requestCount),new Response.Listener<JSONArray>()
{
@Override
public void onResponse(JSONArray response)
{
//Calling method parseData to parse the json response
System.out.println("value of response2 is"+response);
parseData2(response);
//Hiding the progressbar
// progressBar.setVisibility(View.GONE);
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
// progressBar.setVisibility(View.GONE);
//If an error occurs that means end of the list has reached
Toast.makeText(Gallery_byVolly.this, "No More Items Available", Toast.LENGTH_SHORT).show();
}
});
//Returning the request
return jsonArrayRequest;
}
private JsonArrayRequest getDataFromServer(int requestCount)
{
//Initializing ProgressBar
final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar1);
//Displaying Progressbar
progressBar.setVisibility(View.VISIBLE);
setProgressBarIndeterminateVisibility(true);
//JsonArrayRequest of volley
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(DATA_URL + String.valueOf(requestCount),new Response.Listener<JSONArray>()
{
@Override
public void onResponse(JSONArray response)
{
//Calling method parseData to parse the json response
System.out.println("value of response is"+response);
parseData(response);
//Hiding the progressbar
progressBar.setVisibility(View.GONE);
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
progressBar.setVisibility(View.GONE);
//If an error occurs that means end of the list has reached
Toast.makeText(Gallery_byVolly.this, "No More Items Available", Toast.LENGTH_SHORT).show();
}
});
//Returning the request
return jsonArrayRequest;
}
private void parseData2(JSONArray response)
{
for (int i = 0; i < response.length(); i++)
{
//Creating the superhero object
VikasGallery superHero = new VikasGallery();
JSONObject json = null;
try {
//Getting json
json = response.getJSONObject(i);
//Adding data to the superhero object
superHero.setImagesurl(json.getString(Config.TAG_IMAGE_URL));
}
catch (JSONException e)
{
e.printStackTrace();
}
//Adding the superhero object to the list
vollygallerylist2.add(superHero);
System.out.println("value of list2" + vollygallerylist2);
}
//Notifying the adapter that data has been added or changed
adapter2.notifyDataSetChanged();
}
//This method will get data from the web api
private void getData()
{
//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
VikasGallery superHero = new VikasGallery();
JSONObject json = null;
try {
//Getting json
json = array.getJSONObject(i);
//Adding data to the superhero object
superHero.setImagesurl(json.getString(Config.TAG_IMAGE_URL));
}
catch (JSONException e)
{
e.printStackTrace();
}
//Adding the superhero object to the list
gallerylist1.add(superHero);
System.out.println("value of list inside parse m" + gallerylist1);
}
//Notifying the adapter that data has been added or changed
adapter.notifyDataSetChanged();
}
private boolean isLastItemDisplaying(RecyclerView recyclerView)
{
if (recyclerView.getAdapter().getItemCount() != 0)
{
int lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
if (lastVisibleItemPosition != RecyclerView.NO_POSITION && lastVisibleItemPosition == recyclerView.getAdapter().getItemCount() - 1)
return true;
}
return false;
//Overriden method to detect scrolling
// public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY)
// {
// //Ifscrolled at last then
// if (isLastItemDisplaying(gallerygridview1))
// {
//
// getData();
// }
// }
}
适配器类
这是图库类的适配器类,它基本上在recycleview(在listitems中)显示图像后点击图像调用下一个显示完整图像的活动。
package com.example.gallery;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.example.admin.app_shewale.R;
import com.example.vikaskame.VikasGallery;
import java.util.List;
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder>
{
//Imageloader to load image
private ImageLoader imageLoader;
private Context context;
//List to store all superheroes
List<VikasGallery> superHeroes;
//Constructor of this class
public CardAdapter(List<VikasGallery> superHeroes, Context context){
super();
//Getting all superheroes
this.superHeroes = superHeroes;
this.context = context;
System.out.println("value of list adapetr" + superHeroes);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.vollyitemlist, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position)
{
//Getting the particular item from the list
VikasGallery superHero = superHeroes.get(position);
System.out.println("value of lis is sumit kumawat"+superHeroes);
//Loading image from url
imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
imageLoader.get(superHero.getImagesurl(), ImageLoader.getImageListener(holder.imageView, R.drawable.vikas1, android.R.drawable.ic_dialog_alert));
//Showing data on the views
holder.imageView.setImageUrl(superHero.getImagesurl(), imageLoader);
}
@Override
public int getItemCount() {
return superHeroes.size();
}
class ViewHolder extends RecyclerView.ViewHolder
{
//Views
public NetworkImageView imageView;
//Initializing Views
public ViewHolder(View itemView) {
super(itemView);
imageView = (NetworkImageView) itemView.findViewById(R.id.vollyimage);
}
}
}
logcat的 这是显示错误的logatcat
07-27 07:20:47.113 760-3040/? I/ActivityManager: START u0 {cmp=com.example.admin.app_shewale/com.example.gallery.Gallery_byVolly} from uid 10167 on display 0
07-27 07:20:47.116 760-820/? W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client
07-27 07:20:47.129 12543-12543/? I/System.out: value of list adapetr[]
07-27 07:20:47.204 12543-12562/? W/EGL_emulation: eglSurfaceAttrib not implemented
07-27 07:20:47.204 12543-12562/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xeb8c8c80, error=EGL_SUCCESS
07-27 07:20:47.219 12543-12543/? E/RecyclerView: No adapter attached; skipping layout
07-27 07:20:47.219 12543-12543/? E/RecyclerView: No adapter attached; skipping layout
07-27 07:20:47.589 12543-12543/? E/RecyclerView: No adapter attached; skipping layout
07-27 07:20:47.589 12543-12543/? E/RecyclerView: No adapter attached; skipping layout
07-27 07:20:47.621 760-786/? I/ActivityManager: Displayed com.example.admin.app_shewale/com.example.gallery.Gallery_byVolly: +503ms
07-27 07:20:47.654 12543-12543/? I/System.out: value of response is[{"image":"http:\/\/res.cloudinary.com\/djsjqj6l6\/image\/upload\/v1468322269\/gallery\/ashtvinayakdarshan\/gallery1.jpg"},{"image":"http:\/\/res.cloudinary.com\/djsjqj6l6\/image\/upload\/v1468322270\/gallery\/ashtvinayakdarshan\/gallery2.jpg"},{"image":"http:\/\/res.cloudinary.com\/djsjqj6l6\/image\/upload\/v1468322271\/gallery\/ashtvinayakdarshan\/gallery3.jpg"},{"image":"http:\/\/res.cloudinary.com\/djsjqj6l6\/image\/upload\/v1468322272\/gallery\/ashtvinayakdarshan\/gallery4.jpg"}]
07-27 07:20:47.654 12543-12543/? I/System.out: value of list inside parse m[com.example.vikaskame.VikasGallery@1e1d2d90]
07-27 07:20:47.654 12543-12543/? I/System.out: value of list inside parse m[com.example.vikaskame.VikasGallery@1e1d2d90, com.example.vikaskame.VikasGallery@3ae7689]
07-27 07:20:47.654 12543-12543/? I/System.out: value of list inside parse m[com.example.vikaskame.VikasGallery@1e1d2d90, com.example.vikaskame.VikasGallery@3ae7689, com.example.vikaskame.VikasGallery@305c668e]
07-27 07:20:47.654 12543-12543/? I/System.out: value of list inside parse m[com.example.vikaskame.VikasGallery@1e1d2d90, com.example.vikaskame.VikasGallery@3ae7689, com.example.vikaskame.VikasGallery@305c668e, com.example.vikaskame.VikasGallery@1e6b1eaf]
07-27 07:20:47.655 12543-12543/? I/System.out: value of lis is sumit kumawat[com.example.vikaskame.VikasGallery@1e1d2d90, com.example.vikaskame.VikasGallery@3ae7689, com.example.vikaskame.VikasGallery@305c668e, com.example.vikaskame.VikasGallery@1e6b1eaf]
07-27 07:20:47.656 12543-12543/? I/System.out: value of lis is sumit kumawat[com.example.vikaskame.VikasGallery@1e1d2d90, com.example.vikaskame.VikasGallery@3ae7689, com.example.vikaskame.VikasGallery@305c668e, com.example.vikaskame.VikasGallery@1e6b1eaf]
07-27 07:20:47.656 12543-12543/? I/System.out: value of lis is sumit kumawat[com.example.vikaskame.VikasGallery@1e1d2d90, com.example.vikaskame.VikasGallery@3ae7689, com.example.vikaskame.VikasGallery@305c668e, com.example.vikaskame.VikasGallery@1e6b1eaf]
07-27 07:20:47.657 12543-12543/? I/System.out: value of lis is sumit kumawat[com.example.vikaskame.VikasGallery@1e1d2d90, com.example.vikaskame.VikasGallery@3ae7689, com.example.vikaskame.VikasGallery@305c668e, com.example.vikaskame.VikasGallery@1e6b1eaf]
07-27 07:20:47.658 12543-12543/? E/RecyclerView: No adapter attached; skipping layout
07-27 07:20:47.658 12543-12543/? E/RecyclerView: No adapter attached; skipping layout
07-27 07:20:47.995 12543-12543/? E/RecyclerView: No adapter attached; skipping layout
07-27 07:20:47.995 12543-12543/? E/RecyclerView: No adapter attached; skipping layout
07-27 07:20:48.108 12543-12562/? W/EGL_emulation: eglSurfaceAttrib not implemented
07-27 07:20:48.109 12543-12562/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe245b560, error=EGL_SUCCESS
答案 0 :(得分:0)
答案是告诉你究竟是什么问题。 vollygallerygridview2从来没有设置适配器。每个RecyclerView必须有一个适配器。
答案 1 :(得分:-1)
首先,您使用可怕的变量命名样式。切勿对重复变量使用数字(1..2 ... 3 ..)。
您的logcat向您显示,其中一个回收站视图没有适配器。我发现只有一个适配器,但我没有看到另一个适配器。您拨打adapter2.notifyDataSetChanged();
但未初始化。
编辑。
//initializing our adapter
layoutManager = new GridLayoutManager(this,2);
gallerygridview1.setLayoutManager(layoutManager);
getData();
adapter = new CardAdapter(gallerylist1, this);
第二个在哪里?