以卡片形式显示Listview内容

问题描述 投票:0回答:1

我正在寻找一种方法来在我的Listview中显示卡片中的内容,类似于Android Studio中的Google Card或OneFootball App。

我的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();
        }
    }
}

我们是CML:

<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>        

有人可以帮忙吗?

android listview xml-parsing cardview cardslib
1个回答
0
投票

将此布局用于适配器并将适配器设置为列表视图:

把它放在你的gradle中:

编译'com.android.support:cardview-v7:27.1.0'

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="600px"
                android:layout_marginBottom="@dimen/margin_10"
                app:cardBackgroundColor="@color/white">

     .....your desired content


                         </android.support.v7.widget.CardView>
© www.soinside.com 2019 - 2024. All rights reserved.