How to create a shelf like view in Android?

后端 未结 1 1495
予麋鹿
予麋鹿 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

    
    
        
        
    
    

    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)
提交回复
热议问题