如何使用在列表项单击时动态生成的文本视图标签突出显示我在列表视图中的当前位置

How to highlight my current position in list-view using text-view tags generated dynamically on list-item click

我的activity流程是这样的。

**Main ListView**--- list item click

 SubListview- Listitem_1          (parent name-Group)
           Listitem_2

 childListview         Onclick Listitem_1 or 2:     
                             New ListView-new_list_item_1    (parent name-Party)
                                         -new_list_item_2

 grandchildlistview         Onclick new_list_item_1 or 2: 
                   Generate Final-List_view having n elements (parent name-Party name)

现在我采用了线性布局,它位于工具栏下方的列表视图上方。

我已经创建了 headerTextviewpath 方法,它会在单击 Listitem_1 或 2 时触发,并生成动态文本视图。

在此方法中,textview 将设置为单击的项目父名称的文本

我生成的线性布局如下图

群组>派对>派对名称

现在我已经完成了所有这些并且还为动态生成的文本视图分配了颜色。

Goal:: 我想用不同的颜色突出显示线性布局中最后选择的父名称,然后像这样突出显示线性布局中的其他文本视图

子列表视图>(在主列表项上单击)

SubListview> childListview> (Onclick Listitem_1 or 2)

SubListview>childListview>grandchildlistview (Onclick new_list_item_1 or 2)

注意: 我在 textview 中添加父名称作为标签,并将其存储在数组列表中,即 tag_list

动态文本视图生成代码:::::

方法是这样调用的:

ArrayList tag_list=new ArrayList;

headerTextviewpath(listitem_seleted_name, functionName, true);

headerTextviewpath

    public void headerTextviewpath(final String listitem_selected, String parent_name, boolean b) {
    if (b) {
            linearLayout.removeAllViews();
        }
        final TextView tv = new TextView(this);
        tv.setLayoutParams(new 
        LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));

        //tv.setBackgroundResource(R.drawable.button_design);
        tv.setBackgroundColor(getResources().getColor(R.color.DateTheme));
        tv.setText(listitem_selected);

        Drawable next = getResources().getDrawable(R.drawable.ic_keyboard_arrow_right);
        DrawableCompat.setTint(next, ContextCompat.getColor(this, R.color.colorPos6));
        tv.setCompoundDrawablesWithIntrinsicBounds(null, null, next, null);
        tv.setTag(parnet_name);
        tv.setTextColor(getResources().getColor(R.color.colorPos3));
        tag_list.add(parnet_name);

        tv.setPadding(8, 8, 8, 8);

        linearLayout.addView(tv);

       ///using this i am getting current tag of current textview
        int childCount = linearLayout.getChildCount();
        String currentModule = tag.get(childCount - 1);

}

我怎样才能为上述需求做一个逻辑,请帮忙。

首先,当我传递数组列表中的值并发送一个标志时,它会全部记录 到我当前的函数名称与添加到数组列表中的函数名称相匹配的条件 所以我在下面的方法中为它创建了一个简单的函数。

public void headerTextviewpath(final String displayText, String functionCall, boolean b) {

        if (b) {
            linearLayout.removeAllViews();
        }
        linearLayout.setVisibility(View.VISIBLE);
        TextView tv = new TextView(this);
        tv.setFocusable(true);
        tv.setFocusableInTouchMode(true);
        tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));

        //tv.setBackgroundResource(R.drawable.button_design);
        tv.setBackgroundColor(getResources().getColor(R.color.DateTheme));
        tv.setText(FromHtml.getText(displayText));

        Drawable next = getResources().getDrawable(R.drawable.ic_keyboard_arrow_right);
        DrawableCompat.setTint(next, ContextCompat.getColor(this, R.color.colorPos6));
        tv.setCompoundDrawablesWithIntrinsicBounds(null, null, next, null);
        tv.setTag(functionCall);
        tv.setTextColor(getResources().getColor(R.color.colorPos3));
        tag.add(functionCall);

        tv.setPadding(8, 8, 8, 8);

        linearLayout.addView(tv);


        int childCount = linearLayout.getChildCount();


        Object tagObj = linearLayout.getChildAt(0).getTag();
        Log.e("TagObj", "==>" + tagObj.toString() + childCount);

        final String currentModule = tag.get(childCount - 1);
        //  Log.e("TagObjCurr","==>"+currentModule+" "+functionName);
        for (int i = 0; i < tag.size(); i++) {

            Log.e("MoudelFound==>" + i, "tag.get(i):" + tag.get(i) + "  currentModule:" + currentModule);

            if (tag.get(i).equals(currentModule)) {
                tv = linearLayout.findViewWithTag(currentModule);
                tv.setTextColor(getResources().getColor(R.color.colorPos3));
                tv.requestFocus();

            } else {

                tv = linearLayout.findViewWithTag(tag.get(i));
                tv.setTextColor(getResources().getColor(R.color.colorAccent));

            }
        }
 }

最终它运行良好并显示了正确的路径,您还可以显示颜色代码以确定差异。