attempt to make a simple recycler view to show the results of an rss request and parsing

This commit is contained in:
Shinokuni 2019-01-15 18:43:25 +00:00
parent f66e221a5b
commit 4d1d723158
8 changed files with 235 additions and 12 deletions

View File

@ -34,4 +34,10 @@ dependencies {
implementation 'com.github.bumptech.glide:glide:4.8.0' implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.retrofit2:converter-simplexml:2.4.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
} }

View File

@ -2,26 +2,102 @@ package com.readrops.app;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log; import android.util.Log;
import com.readrops.readropslibrary.PageParser; import com.readrops.readropslibrary.PageParser;
import com.readrops.readropslibrary.Utils.Utils;
import com.readrops.readropslibrary.localfeed.RSSNetwork;
import com.readrops.readropslibrary.localfeed.rss.RSSFeed;
import com.readrops.readropslibrary.localfeed.rss.RSSItem;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
String url = "https://framablog.org/"; String url = "https://framablog.org/";
private RecyclerView recyclerView;
private MainAdapter adapter;
private List<RSSItem> itemList;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
Thread thread = new Thread(()-> { /*Thread thread = new Thread(() -> {
String feedUrl = PageParser.getFeedLink(url); String imageUrl = PageParser.getOGImageLink("https://usbeketrica.com/galerie/dennis-osadebe-portrait-of-a-bright-generation");
Log.d("", ""); Log.d("", "");
runOnUiThread(() -> {
getItems();
});
}); });
thread.start(); thread.start();*/
getItems();
}
private void getItems() {
RSSNetwork request = new RSSNetwork();
request.request("https://www.numerama.com/feed/", new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
InputStream stream = response.body().byteStream();
String xml = Utils.inputStreamToString(stream);
Serializer serializer = new Persister();
try {
RSSFeed rssFeed = serializer.read(RSSFeed.class, xml);
itemList = rssFeed.getChannel().getItems();
runOnUiThread(() -> {
initRecyclerView();
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
private void initRecyclerView() {
recyclerView = findViewById(R.id.items_recycler_view);
adapter = new MainAdapter(this, itemList);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
DividerItemDecoration decoration = new DividerItemDecoration(this, ((LinearLayoutManager) layoutManager).getOrientation());
recyclerView.addItemDecoration(decoration);
recyclerView.setAdapter(adapter);
} }

View File

@ -0,0 +1,73 @@
package com.readrops.app;
import android.content.Context;
import android.media.Image;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.readrops.readropslibrary.PageParser;
import com.readrops.readropslibrary.localfeed.rss.RSSItem;
import java.util.List;
public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {
List<RSSItem> items;
private Context context;
public MainAdapter(Context context, List<RSSItem> items) {
this.context = context;
this.items = items;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
View view = inflater.inflate(R.layout.compact_list_element, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
RSSItem item = items.get(i);
viewHolder.bind(item);
Thread thread = new Thread(() -> {
String imageUrl = PageParser.getOGImageLink(item.getLink());
Glide.with(context).load(imageUrl).into(viewHolder.itemImage);
});
}
@Override
public int getItemCount() {
return items.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
private TextView itemTitle;
private ImageView itemImage;
ViewHolder(@NonNull View itemView) {
super(itemView);
itemTitle = itemView.findViewById(R.id.item_title);
itemImage = itemView.findViewById(R.id.item_image);
}
private void bind(RSSItem item) {
itemTitle.setText(item.getTitle());
}
}
}

View File

@ -6,13 +6,13 @@
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity"> tools:context=".MainActivity">
<TextView <android.support.v7.widget.RecyclerView
android:layout_width="wrap_content" android:id="@+id/items_recycler_view"
android:layout_height="wrap_content" android:layout_width="match_parent"
android:text="Hello World!" android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent" android:scrollbars="vertical">
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" /> </android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout> </android.support.constraint.ConstraintLayout>

View File

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="qsdsdsfdksfdsfjdsklfdjsfdslkfjdsfjdsfjldksfjdsfdskfjdslfdksfkldsfdsfdsqd"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:layout_marginTop="12dp"
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp"
android:minLines="1"
android:maxLines="3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@id/item_image"
android:layout_toStartOf="@id/item_image"
/>
<ImageView
android:id="@+id/item_image"
android:layout_width="150dp"
android:layout_height="70dp"
android:layout_marginTop="12dp"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:layout_alignParentEnd="true"
android:layout_marginBottom="12dp"
/>
</RelativeLayout>

View File

@ -0,0 +1,13 @@
package com.readrops.readropslibrary.Utils;
import java.io.InputStream;
import java.util.Scanner;
public final class Utils {
public static String inputStreamToString(InputStream input) {
Scanner scanner = new Scanner(input).useDelimiter("\\A");
return scanner.hasNext() ? scanner.next() : "";
}
}

View File

@ -0,0 +1,17 @@
package com.readrops.readropslibrary.localfeed;
import okhttp3.OkHttpClient;
import okhttp3.Request;
public class RSSNetwork {
public void request(String url, okhttp3.Callback callback) {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
okHttpClient.newCall(request).enqueue(callback);
}
}