我正在寻找一种方法来在我的 Listview 中的卡片中显示内容,类似于 Google Card 或 Android Studio 中的 OneFootball 应用程序。
我的 MainActivty.java:
public class MainActivity extends AppCompatActivity {
private Button button;
ListView lvRSS;
ArrayList<String> titles;
ArrayList<String> links;
ArrayList<String> img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
button = findViewById(R.id.navBar);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openMenu();
}
});
lvRSS = findViewById(R.id.lvRSS);
titles = new ArrayList<>();
links = new ArrayList<>();
img = new ArrayList<>();
lvRSS.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Uri uri = Uri.parse(links.get(position));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
new ProcessInBackground().execute();
}
public void openMenu() {
Intent intentMenu = new Intent(this, menuActivity.class);
startActivity(intentMenu);
}
public InputStream getInputStream(URL url)
{
try
{
return url.openConnection().getInputStream();
}
catch (IOException e)
{
return null;
}
}
public class ProcessInBackground extends AsyncTask<Integer, Void, Exception>
{
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
Exception exception = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.setMessage("Loading");
progressDialog.show();
}
@Override
protected Exception doInBackground(Integer... params) {
try
{
URL url = new URL("http://www.scotscollege.school.nz/category/sports-news/feed/");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xmlPullParser = factory.newPullParser();
xmlPullParser.setInput(getInputStream(url), "UTF_8");
boolean insideItem = false;
int eventType = xmlPullParser.getEventType();
while (eventType != xmlPullParser.END_DOCUMENT)
{
if (eventType == XmlPullParser.START_TAG)
{
if (xmlPullParser.getName().equalsIgnoreCase("item"))
{
insideItem = true;
}
else if (xmlPullParser.getName().equalsIgnoreCase("title"))
{
if (insideItem)
{
titles.add(xmlPullParser.nextText());
}
}
else if (xmlPullParser.getName().equalsIgnoreCase("guid"))
{
if (insideItem)
{
links.add(xmlPullParser.nextText());
}
}
else if (xmlPullParser.getName().equalsIgnoreCase("img"))
{
if (insideItem)
{
img.add(xmlPullParser.nextText());
}
}
}
else if (eventType == XmlPullParser.END_TAG && xmlPullParser.getName().equalsIgnoreCase("item"))
{
insideItem = false;
}
eventType = xmlPullParser.next();
}
}
catch (MalformedURLException e)
{
exception = e;
}
catch (XmlPullParserException e)
{
exception = e;
}
catch (IOException e)
{
exception = e;
}
return exception;
}
@Override
protected void onPostExecute(Exception s) {
super.onPostExecute(s);
ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, titles);
lvRSS.setAdapter(adapter);
progressDialog.dismiss();
}
}
}
我的 XML:
<android.support.constraint.ConstraintLayout 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"
android:background="@drawable/background"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/scLogo"
android:layout_width="51dp"
android:layout_height="46dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@drawable/sc"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.013"
app:srcCompat="@drawable/sc" />
<ImageView
android:id="@+id/homeTitle"
android:layout_width="80dp"
android:layout_height="30dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="25dp"
android:contentDescription="@string/app_name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/home_big" />
<Button
android:id="@+id/navBar"
android:layout_width="48dp"
android:layout_height="44dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/ic_menu"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/scLogo"
app:layout_constraintHorizontal_bias="0.023"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.013" />
<ListView
android:id="@+id/lvRSS"
style="@style/Widget.AppCompat.ListView"
android:layout_width="395dp"
android:layout_height="581dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/scLogo"/>
</android.support.constraint.ConstraintLayout>
任何人都可以帮忙吗?