简体   繁体   English

ANDROID:创建一个类似于dataGrid的视图,但改为使用LinearLayouts

[英]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. 我遇到了一个问题,我无法将我的dataGridView放到scrollView中,并且如果我有很多列,它们只会变得很薄,以致无法在那里看到任何东西。 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. 这就是为什么我决定重新制作它并为每列创建带有Vertical Layout的 LinearLayout,并且它们中的每一个都将具有另一个带有Horizo​​ntal Layout的 LinearLayout只是为了模拟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 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) PageFragment.java(应填写LinearLayout的位置)

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. 不要使用自定义适配器,请使用CustomAdapter的getView()方法for循环以设置数据。
  • Cast your Linearlayout which id is main_grid_layout by `findViewById(). 通过`findViewById() Linearlayout id为main_grid_layout

  • add convertView of getView() method to main_grid_layout getView()方法的convertView添加到main_grid_layout

Skip Item Android Grid View in condition 跳过项目Android网格视图

hope it will help you 希望对你有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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