在第一行和第一列上使用Textview的动态网格布局

时间:2014-05-26 05:33:47

标签: android forms layout dynamic grid

我在动态设计表单时遇到问题。它的表格形式如下:

|        |  Great |  Good | Average | Bad |
|Quality |        |   -   |         |     |
|Perform |        |       |    -    |     |
|Support |    -   |       |         |     |

第一行(标题行)和第一列是动态的,即行和列可以增加或减少,并且将来自Web服务。其他单元格将像按钮一样,只有一个按钮将被激活( - )连续。

有人知道如何实现这个目标吗?

这里我使用线性布局做了什么:这只是一个模拟数据,我没有实现任何点击事件或捕获用户输入(我不知道该怎么做)是否涉及管理ID。

截图: https://drive.google.com/file/d/0BzbVBFzhlWapbzlFeWlJSVF5M1k/edit?usp=sharing

我使用的对象模型是真实对象模型的副本。

//Table Form Type Layout
private void createFormWithLinearLayouts() 
{
    //Get Feedback Answer List
    ArrayList<FeedbackAnswer> feedbackAnswerList = feedbackQuestionSection.getFeedbackAnswerList();

    //Get Feedback Question List
    ArrayList<FeedbackQuestion> feedbackQuestionList = feedbackQuestionSection.getFeedbackQuestionList();

    //For loop for Questions
    for(int i= -1; i< feedbackQuestionList.size();i++)
    {
        LinearLayout cLayout = new LinearLayout(this);
        cLayout.setOrientation(LinearLayout.HORIZONTAL);
        cLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        MarginLayoutParams  params = new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.setMargins(5, 5, 5, 5);

        //For loop for AAnswers
        for (int j = -1; j < feedbackAnswerList.size(); j++)
        {
            TextView tv;
            tv = new TextView(this);
            tv.setWidth(100);
            tv.setHeight(50);
            tv.setLayoutParams(params);

            if(i == -1 && j== -1)
            {
                tv.setText(" ");
            }
            else if(i == -1 && j!= -1)
            {
                tv.setText(feedbackAnswerList.get(j).getDesc());
            }
            else if(j != -1)
            {
                //Buttons or Simply Color Change for Select or DeSelect

                //Get Answer from Question
                FeedbackAnswer feedbackAnswerOfQuestion = feedbackQuestionList.get(i).getFeedbackAnswer();

                if(feedbackAnswerList.get(j).getSeqNo() == feedbackAnswerOfQuestion.getSeqNo())
                {
                    tv.setText(feedbackAnswerList.get(j).getDesc()); //The PreSelected Answer will Come from Server
                }
                else
                {
                    tv.setText(" - - -");
                }
            }
            else
            {
                tv = new TextView(this);
                tv.setWidth(80);
                tv.setHeight(50);
                tv.setLayoutParams(params);

                tv.setText(feedbackQuestionList.get(i).getDesc());
            }

            cLayout.addView(tv);
        }


        parentLayout.addView(cLayout);
    }
}

/ * 在对象模型中准备的数据

//Prepare Feedback Question Section 
public void prepareFeedbackQuestionSection()
{
    //Question Section (Whole Layout)
    feedbackQuestionSection = new FeedbackQuestionSection();


    /******** Answers will show as Header row **************/
    FeedbackAnswer feedbackAnswer1 = new FeedbackAnswer();
    feedbackAnswer1.setDesc("Great"); 
    feedbackAnswer1.setSeqNo(1);


    FeedbackAnswer feedbackAnswer2 = new FeedbackAnswer();
    feedbackAnswer2.setDesc("Good"); 
    feedbackAnswer2.setSeqNo(2);

    FeedbackAnswer feedbackAnswer3 = new FeedbackAnswer();
    feedbackAnswer3.setDesc("Average"); 
    feedbackAnswer3.setSeqNo(3);

    FeedbackAnswer feedbackAnswer4 = new FeedbackAnswer();
    feedbackAnswer4.setDesc("Bad"); 
    feedbackAnswer4.setSeqNo(4);

    //Answer List (Row Header Texts)
    ArrayList<FeedbackAnswer> feedbackAnswerList = new ArrayList<FeedbackAnswer>();
    feedbackAnswerList.add(feedbackAnswer1);
    feedbackAnswerList.add(feedbackAnswer2);
    feedbackAnswerList.add(feedbackAnswer3);
    feedbackAnswerList.add(feedbackAnswer4);

    /*** Questions will show as First Column Texts **************/
    FeedbackQuestion feedbackQuestion1  = new FeedbackQuestion();
    feedbackQuestion1.setDesc("Quality"); 
    feedbackQuestion1.setSeqNo(1);
    feedbackQuestion1.setFeedbackAnswer(feedbackAnswer3);

    FeedbackQuestion feedbackQuestion2  = new FeedbackQuestion();
    feedbackQuestion2.setDesc("Performance"); 
    feedbackQuestion2.setSeqNo(2);
    feedbackQuestion2.setFeedbackAnswer(feedbackAnswer2);

    FeedbackQuestion feedbackQuestion3  = new FeedbackQuestion();
    feedbackQuestion3.setDesc("Support"); 
    feedbackQuestion3.setSeqNo(3);
    feedbackQuestion3.setFeedbackAnswer(feedbackAnswer4);


    //Question List (First Column Texts)
    ArrayList<FeedbackQuestion> feedbackQuestionList = new ArrayList<FeedbackQuestion>();
    feedbackQuestionList.add(feedbackQuestion1);
    feedbackQuestionList.add(feedbackQuestion2);
    feedbackQuestionList.add(feedbackQuestion3);


    //Add to Question Section
    feedbackQuestionSection.setFeedbackAnswerList(feedbackAnswerList);
    feedbackQuestionSection.setFeedbackQuestionList(feedbackQuestionList);
}

/ **布局* /

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

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

/ ** OnCreate()* /     @覆盖     protected void onCreate(Bundle savedInstanceState)     {         super.onCreate(savedInstanceState);

    setContentView(R.layout.feedback_form_dynamic);

    //Get Linear Layout
    parentLayout = (LinearLayout) findViewById(R.id.layout);

    //Prepare Data
    prepareFeedbackQuestionSection();

    //Create Form
    createFormWithLinearLayouts();

}

2 个答案:

答案 0 :(得分:0)

经过大量搜索并阅读网格视图后,我使用动态内容制作了gridview,并处理了项目点击事件以捕获用户输入。

屏幕截图:https://drive.google.com/file/d/0BzbVBFzhlWapTG9vbWREZllNeFU/edit?usp=sharing

基本上有两个数组列表:问题列表和答案列表,两者都包含&#39;描述&#39;和&#39; sequenceNo&#39; (或等级)。

/ *适配器 * /

private class FeedbackAdapter extends BaseAdapter
{
    ArrayList<FeedbackAnswer> feedbackAnswerList;
    ArrayList<FeedbackQuestion> feedbackQuestionList;

    public FeedbackAdapter(FeedbackQuestionSection fSection) 
    {
        feedbackQuestionList = fSection.getFeedbackQuestionList();
        feedbackAnswerList = fSection.getFeedbackAnswerList();
    }

    @Override
    public int getCount() 
    {   
        return ((feedbackAnswerList.size() +1 )* ( feedbackQuestionList.size() +1 ));
    }

    @Override
    public Object getItem(int index) 
    {
        if(index == 0)
        {
            return null;
        }
        else if(index <= feedbackAnswerList.size())
        {
            return feedbackAnswerList.get(index - 1);
        }
        else // if(index % (feedbackAnswerList.size() + 1) == 0)
        {
            return feedbackQuestionList.get((index) / (feedbackAnswerList.size() + 1) - 1);
        }
    }

    @Override
    public long getItemId(int position) 
    {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        View rowView = convertView;

        if(rowView==null)
        {
            LayoutInflater layoutInflater=LayoutInflater.from(DynamicFormDemo.this);
            rowView = layoutInflater.inflate(android.R.layout.simple_list_item_1, parent,false);
        }

        TextView textView=(TextView) rowView.findViewById(android.R.id.text1);

        //Get Item
        QuestionPaperInterface question = (QuestionPaperInterface) getItem(position);

        if(question != null)
        {
            if(question instanceof FeedbackAnswer)
            {
                question = (FeedbackAnswer) question;
                textView.setText(((FeedbackAnswer) question).getDesc());
            }
            else if(question instanceof FeedbackQuestion)
            {
                question = (FeedbackQuestion) question;

                if(position % (feedbackAnswerList.size() + 1) == 0)
                {
                    textView.setText(((FeedbackQuestion) question).getDesc());
                }
                else
                {
                    //Tag Answer
                    textView.setTag(R.id.feedbackAnswer, feedbackAnswerList.get(position % (feedbackAnswerList.size() + 1) - 1));

                    //Tag Question
                    textView.setTag(R.id.feedbackQuestion, ((FeedbackQuestion) question));

                    FeedbackAnswer feedbackAnswer = ((FeedbackQuestion) question).getFeedbackAnswer();
                    int seq = feedbackAnswer.getSeqNo();

                    //Get Tagged Answer
                    FeedbackAnswer taggedFeedbackAnswer = (FeedbackAnswer) textView.getTag(R.id.feedbackAnswer);

                    int taggedSeqNo = taggedFeedbackAnswer.getSeqNo();

                    if(seq == taggedSeqNo)
                    {
                        textView.setText(feedbackAnswer.getDesc());
                    }
                    else
                    {
                        textView.setText("---");
                    }
                }
            }
        }
        else
        {
            textView.setText("---");
        }

        return rowView;
    }
}

/ *在项目上点击 //

    @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
{
    //Get View
    TextView textView = (TextView) view;

    //Get Feedback Answer list
    ArrayList<FeedbackAnswer> feedbackAnswerList = feedbackQuestionSection.getFeedbackAnswerList();

    if(position >= feedbackAnswerList.size() + 1 && position % (feedbackAnswerList.size() + 1) != 0)
    {
        //Get Tagged Feedback Question
        FeedbackQuestion feedbackQuestion = (FeedbackQuestion) textView.getTag(R.id.feedbackQuestion);

        //Get Tagged Feedback Answer
        FeedbackAnswer feedbackAnswer = (FeedbackAnswer) textView.getTag(R.id.feedbackAnswer);

        //Set New Answer in Question
        feedbackQuestion.setFeedbackAnswer(feedbackAnswer);

        //Notify Adapter
        feedbackAdapter.notifyDataSetChanged();

        String toastMessage = "Feedback Answer is :" + feedbackAnswer.getSeqNo() + ": "+feedbackAnswer.getDesc();
        Toast toast = Toast.makeText(this, toastMessage, Toast.LENGTH_SHORT);
        toast.show();
    }
}

/ * 在On Create() * /

    int numOfColms = feedbackQuestionSection.getFeedbackAnswerList().size() + 1; //No of Columns = No of Answers + 1 (Blank first Row First Column)

    GridView gridView = (GridView) findViewById(R.id.gridview);
    gridView.setOnItemClickListener(this);

    gridView.setStretchMode(GridView.NO_STRETCH);  
    gridView.setNumColumns(numOfColms);  

但它并不符合我的要求,这个网格视图滚动垂直,我不需要,因为上面和下面有更多的组件像一个垂直滚动的整个反馈形式。所以我正在尝试使用表格的另一个解我发布这个希望它可以帮助某人。

答案 1 :(得分:0)

我通过使用表来解决问题,动态添加行和列。该表包含在水平布局中,整个布局在ListView中。原因是表和其他组件可能会重复并动态更改排序。