简体   繁体   中英

ANDROID: create a view like a dataGrid but made with LinearLayouts instead

I faced with a problem that i cannot put my dataGridView into scrollView and in case I have a lot of columns they just become so thin that it's impossible to see something there. That's why I decided to remake it and create LinearLayout with Vertical Layout for each column and each of them will have another LinearLayout with Horizontal Layout just to simulate GridView. (Hope it's a good idea)
But unfortunately I'm facing some problems during its creation. It is not being created and my application turning off. Ask you for your help

Here is my code:

grid_container.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
    <ScrollView
        android:id="@+id/GridScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/main_grid_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">

            </LinearLayout>
    </ScrollView>

PageFragment.java (place where LinearLayout should be fill out)

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.grid_container, container, false);
    LinearLayout mainLayout = (LinearLayout) view.findViewById(R.id.main_grid_layout);

    int colCount = mPage.get(0).split(",").length;
    int rowCount = mPage.size();

    ArrayList<ArrayList<String>> tempNormList = createNormList(mPage);


    for(int currCol=0;currCol<colCount;currCol++){
        LinearLayout linearLayout = new LinearLayout(view.getContext());
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        linearLayout.setLayoutParams(llParams);

        for(int currRow=0; currRow<rowCount;rowCount++){
            TextView textView = new TextView(view.getContext());
            textView.setText(tempNormList.get(currCol).get(currRow));
            if(currRow==0){
                textView.setBackgroundResource(R.drawable.header_borders);
            }
            linearLayout.addView(textView);
        }
        mainLayout.addView(linearLayout);
    }

    return view;
}

Thank you in advance for your help

try my linked Answer, it will provide your solution, but you have do some changes like below

  • Don't Use Custom Adapter, use for loop with getView() method of CustomAdapter to Set Data.
  • Cast your Linearlayout which id is main_grid_layout by `findViewById().

  • add convertView of getView() method to main_grid_layout

Skip Item Android Grid View in condition

hope it will help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM