Ich versuche, eine kleine App zu machen, die zufällige Bilder und Filme von Hunden und zeigt sie in einer Recycling-Ansicht. Ich benutze Picasso zum Laden der Bilder von URLsRecyclerView - Lade Bilder aus dem Web
Die Art, wie ich es entworfen habe, ist, dass jedes Bild selbst angezeigt wird (Aufnahme der gesamten Recyclerview) und wenn der Benutzer scrollt nach oben oder unten sieht er die vorherige oder nächste Bild jeweils.
Momentan verwende ich einfach eine AsyncTask, um 10 Bilder von https://random.dog/woof.json zu erhalten, lade sie auf eine ArrayList
und verschiebe die Daten auf den Adapter. Funktioniert momentan nicht wie beabsichtigt (Mehrere Bilder in den Raum, wo ich nur einen zeigen möchte, und aus irgendeinem seltsamen Grund wird meine AsyncTask wiederholt aufgerufen, obwohl ich sie nur von meiner MainActivity aus anrufe)
Code für den Adapter :
public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.DogViewHolder> {
public ArrayList<String> urlList = new ArrayList<>();
public Context mContext;
public RecycleViewAdapter(Context context) {
mContext = context;
}
@Override
public DogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
int layoutForListItems = R.layout.list_items;
LayoutInflater inflater =LayoutInflater.from(context);
View view = inflater.inflate(layoutForListItems, null);
return new DogViewHolder(view);
}
@Override
public void onBindViewHolder(final DogViewHolder holder, int position) {
String url = null;
if (urlList != null) {
url = urlList.get(position);
}
if (url != null) {
String imageType = url.substring(url.length() - 4);
View v = new View(mContext);
Log.d("##############", "IMAGE TYPE - " + imageType);
switch (imageType) {
case ".mp4":
Log.d("########", "movie url - " + url);
holder.mVidView.setVideoURI(Uri.parse(url));
holder.mVidView.setVisibility(View.VISIBLE);
holder.mVidView.requestFocus();
holder.mVidView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
holder.mVidView.start();
}
});
holder.mVidView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.d("########", "Error playing movie...");
return false;
}
});
break;
default:
Picasso.with(mContext).load(url).into(holder.mImgView);
holder.mImgView.setVisibility(View.VISIBLE);
}
}
else {
Log.d("###############", "url was null");
holder.mImgView.setImageResource(R.drawable.no_image_found);
holder.mImgView.setVisibility(View.VISIBLE);
}
}
@Override
public int getItemCount() {
if (urlList == null) { return 0; }
return urlList.size();
}
public void updateData(ArrayList<String> newList) {
urlList.addAll(newList);
notifyDataSetChanged();
}
public class DogViewHolder extends RecyclerView.ViewHolder {
private final VideoView mVidView;
private final ImageView mImgView;
private DogViewHolder(View v) {
super(v);
mVidView = (VideoView) v.findViewById(R.id.video_view);
mImgView = (ImageView) v.findViewById(R.id.image_view);
}
}
}
AsyncTask Code: (in OnCreate aufgerufen wird)
public class AsyncTaskThingy extends AsyncTask<Void, Void, ArrayList<String>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected ArrayList<String> doInBackground(Void... params) {
ArrayList<String> urlList = new ArrayList<>();
for(int i = 0; i < 10; i ++) {
urlList.add(DogGrabber.getUrlFromJson());
}
return urlList;
}
@Override
protected void onPostExecute(ArrayList<String> data) {
mAdapter.updateData(data);
super.onPostExecute(data);
declareReady();
}
Ich habe schon ein paar Stunden damit angefangen, als ich .fit() zur Picasso-Lade-Methode hinzufüge, zeigt nichts an.
Gedanken würde ich meine XML-Dateien enthalten aswell:
Hauptaktivität Layout-Datei:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:background="@android:color/holo_blue_light">
<android.support.v7.widget.RecyclerView android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:layout_gravity="center"
android:layout_margin="10dp"
android:background="@drawable/background"
xmlns:android="http://schemas.android.com/apk/res/android">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
Und die RecyclerView Artikel:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
android:id="@+id/image_view"
android:scaleType="centerCrop"/>
<VideoView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:id="@+id/video_view"
/>
</LinearLayout>
make linearlayout height to match_parent – USKMobility
Versucht, dass vorher, nicht gehen. –