回收站视图与内容不匹配

时间:2019-12-17 00:12:58

标签: java android android-recyclerview

我的Android应用程序有问题。 RecyclerView与内容不匹配。它仅与第一个元素匹配,如果我要查看下一个元素,则需要向下滚动。怎么了?

activity_offer_details.xml

.slideshow__slide {
  overflow: hidden;
  height: 359px;
  position: relative;
}

.slide__bg {
  position: absolute;
  width: 100%;
  margin: 0;
  color: #fff;
  background: #333;
  overflow-x: hidden;
  -webkit-box-shadow: inset 0 0 0 1px #f0f0f0;
  box-shadow: inset 0 0 0 1px #f0f0f0;
  transition: all 1.5s ease;
}

.slide__bg img {
  display: block;
}

OfferDetailsActivity.java(我在getPhotos()类中使用RecyclerView)

<section class="slideshow__slide slide">
  <figure class="slide__bg current">
    <img src="https://via.placeholder.com/620x200/070" />
  </figure>
  <figure class="slide__bg">
    <img src="https://via.placeholder.com/620x200/700" />
  </figure>
  <figure class="slide__bg">
    <img src="https://via.placeholder.com/620x200/007" />
  </figure>
</section>

PhotoAdapter.java

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".OfferDetailsActivity">

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

(...)           

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/photos"
                android:layout_marginTop="15dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/login" />

        </RelativeLayout>
    </androidx.core.widget.NestedScrollView>

</RelativeLayout>

PhotoModel.java

package com.example.wynajem;

(...)

public class OfferDetailsActivity extends AppCompatActivity {


    private RecyclerView photos;
    private List<PhotoModel> photoList;
    private String URL_GET_PHOTOS = "http://192.168.8.100/kwadrat/getphotos.php";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_offer_details);

        photos = findViewById(R.id.photos);

        photos.setHasFixedSize(true);
        photos.setLayoutManager(new LinearLayoutManager(this));

        photoList = new ArrayList<>();

        Intent intent = getIntent();
        String mId = intent.getStringExtra("id");

        getPhotos(mId);
    }

(...)

    private void getPhotos(String id) {
        StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_GET_PHOTOS,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONArray array = new JSONArray(response);

                            for(int i=0; i<array.length(); i++) {
                                JSONObject onePhoto = array.getJSONObject(i);

                                photoList.add(new PhotoModel(
                                        onePhoto.getString("path").trim()
                                ));
                            }
                            PhotoAdapter adapter = new PhotoAdapter(OfferDetailsActivity.this, photoList);
                            photos.setAdapter(adapter);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {}
                })
        {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("id", id);
                return params;
            }
        };

        Volley.newRequestQueue(this).add(stringRequest);
    }
}

one_photo.xml

package com.example.wynajem.Adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.wynajem.Models.PhotoModel;
import com.example.wynajem.R;
import com.squareup.picasso.Picasso;

import java.util.List;

public class PhotoAdapter extends RecyclerView.Adapter<PhotoAdapter.PhotoViewHolder> {

    private Context mCtx;
    private List<PhotoModel> photoList;

    public PhotoAdapter(Context mCtx, List<PhotoModel> photoList) {
        this.mCtx = mCtx;
        this.photoList = photoList;
    }

    @Override
    public PhotoAdapter.PhotoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mCtx);
        View view = inflater.inflate(R.layout.one_photo, null);
        return new PhotoAdapter.PhotoViewHolder(view);
    }

    @Override
    public void onBindViewHolder(PhotoAdapter.PhotoViewHolder holder, int position) {
        PhotoModel photoModel = photoList.get(position);

        Picasso.get().load(photoModel.getPath()).into(holder.imageViewPhoto);

    }

    @Override
    public int getItemCount() {
        return photoList.size();
    }

    class PhotoViewHolder extends RecyclerView.ViewHolder {
        public ImageView imageViewPhoto;

        public PhotoViewHolder(View itemView) {
            super(itemView);

            imageViewPhoto = itemView.findViewById(R.id.photo);
        }
    }
}

0 个答案:

没有答案