[英]Issues in Picasso Image Loading in Recycler View
實際上,當我單擊卡片查看項目時,它不顯示圖像,而是使用json
和picasso
精確加載的數據。請問有什么錯誤可以解決您在picasso
庫中的輝煌問題?
public class SeconActivity extends Activity
{
RecyclerView recyclerView;
List<Shop> shops;
String serverUrl = "http://yoursubshop.com/webservices/categories-shop.php? category=";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seocnd_main);
recyclerView = (RecyclerView) findViewById(R.id.secondmain);
LinearLayoutManager ll = new LinearLayoutManager(SeconActivity.this);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(ll);
Intent in = getIntent();
String serverUrlId = in.getStringExtra("ks");
serverUrl = serverUrl.concat(serverUrlId);
new JsonTask().execute();
}
public class JsonTask extends AsyncTask<String, String, List<Shop>> {
@Override
protected List<Shop> doInBackground(String... params) {
HttpURLConnection httpURLConnection = null;
BufferedReader bufferedReader = null;
try {
URL url = new URL(serverUrl);
httpURLConnection = (HttpURLConnection) url.openConnection();
InputStreamReader inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
String finaljson = sb.toString();
JSONObject firstobject = new JSONObject(finaljson);
JSONArray firstarray = firstobject.getJSONArray("data");
/* List<Your> yo = new ArrayList<>();
for (int i = 0; i < firstarray.length(); i++) {
JSONObject finalobject = firstarray.getJSONObject(i);
Your y = new Your();
y.setCategory_id(finalobject.getString("category_id"));
y.setCategory_name(finalobject.getString("category_name"));
yo.add(y);*/
List<Shop> yo = new ArrayList<>();
for (int i = 0; i < firstarray.length(); i++) {
JSONObject finalobject = firstarray.getJSONObject(i);
Shop yos = new Shop();
yos.shop_id = finalobject.getString("shop_id");
yos.shop_name= finalobject.getString("shop_name");
yos.Shop_logo=finalobject.getString("shop_logo");
yo.add(yos);
}
return yo;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(List<Shop> result) {
super.onPostExecute(result);
Rvadapter listAdapter = new Rvadapter(SeconActivity.this, result);
recyclerView.setAdapter(listAdapter);
}
}
public class Rvadapter extends RecyclerView.Adapter<Rvadapter.Myadapter> {
List<Shop> yours = Collections.emptyList();
Context c;
public class Myadapter extends RecyclerView.ViewHolder {
TextView id;
TextView name;
ImageView image;
CardView cv;
public Myadapter(final View itemView) {
super(itemView);
id = (TextView) itemView.findViewById(R.id.shopt);
name = (TextView) itemView.findViewById(R.id.shopr);
image = (ImageView)itemView.findViewById(R.id.image);
cv = (CardView)itemView.findViewById(R.id.card);
}
}
public Rvadapter(Context c, List<Shop> yours) {
this.yours = yours;
this.c = c;
}
@Override
public Myadapter onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.shopss, parent, false);
Myadapter m = new Myadapter(v);
return m;
}
@Override
public void onBindViewHolder(Myadapter holder, int position) {
Shop y = yours.get(position);
holder.id.setText(y.shop_id);
holder.name.setText(y.shop_name);
Picasso.with(c)
.load(y.Shop_logo)
.into(holder.image);
}
@Override
public int getItemCount() {
return yours == null ? 0 : yours.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
}
當使用滑行時,圖像視圖的大小應該是預定義的或與父項匹配。 當滑行無法估算身高或體重時,它不會加載任何東西。 因此,請在適配器中放置imageview的高度和重量的負載。
如果確實存在尺寸測量問題,則可以使用Picasso庫更正該問題。
從這里您將獲得詳細的指南,以區分這兩個庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.