How to create a shelf like view in Android?

后端 未结 1 1498
予麋鹿
予麋鹿 2021-01-30 09:42

How to create a shelf like view in android that show several book in any row? Also, it should have horizontal and vertical features like the moon+reader app has.

I can w

相关标签:
1条回答
  • 2021-01-30 10:13

    Last Updated: Now, I can detect a new way for create shelf-view better than the previous solution. I described it in CodeProject

    By the Way, In this application I used two classes:

    • HorizontalListView Class that extends the AdapterView. It downloaded from GitHub

    • Quaere library use almost same as Linq2Object in .Net. You can download here.


    Apr 22 '12:

    There are some ways to implement shelf view that it have two features(horizontal & vertical scroll). I try to write a program that can run dynamically. This sample App have a XML file and a showShelfView java class.

    So you can see my App:

    main XML file: First, Add following code in main.XML

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/sclView">
        <TableLayout
                android:id="@+id/tblLayout"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" 
               android:padding="0dp">
        </TableLayout>
    </ScrollView>
    

    showShelfView Class: Inner TableLayout add several HorizontalScroll equals with number of rows. Also inner any TableRow add Image.

    Don't forget set a shelf image for Row's background:

    enter image description here

    public class showShelfView extends Activity {
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
    
            int numRow = 4;
            int numCol = 8;
    
            TableLayout tblLayout = (TableLayout) findViewById(R.id.tblLayout);
    
            for(int i = 0; i < numRow; i++) {
                HorizontalScrollView HSV = new HorizontalScrollView(this);
                HSV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    
                TableRow tblRow = new TableRow(this);
                tblRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
                tblRow.setBackgroundResource(R.drawable.bookshelf);
    
                for(int j = 0; j < numCol; j++) {
                ImageView imageView = new ImageView(this);
                    imageView.setImageResource(R.drawable.book1);
    
                    TextView textView = new TextView(this);
                    textView.setText("Java Tester");
                    textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    
                    tblRow.addView(imageView,j);
                }
    
                HSV.addView(tblRow);
                tblLayout.addView(HSV, i);
            }
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题