2017-02-17 56 views
0

我需要的是:enter image description here 上面的屏幕是我的抽屜。在那個可擴展的列表視圖中,如果我按下「DashBoard」標題列表,我將重定向到儀表板活動。在這裏,我需要「儀表板」文本爲藍色和其他文本是黑色的。當我點擊「今天的跟進」childview我想要「CRM」和「今日跟進」文本到藍色和其他人是黑色。 我的抽屜式導航欄活動:如何在可擴展列表視圖中更改選定列表項和標題和子項的textcolor?

public class BaseActivity extends AppCompatActivity { 

    protected DrawerLayout drawer; 
    ExpandableListView expListView; 
    ExpandableListAdapter listAdapter; 
    HashMap<String, List<String>> listDataChild,listDatachild2; 
    List<String> listDataHeader; 
    List<String> listDataHeader2; 
    public List<Integer> groupImages; 
    private NavigationView navigationView; 
    ViewStub viewStub; 

    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_testingnavi); 

    } 

    public void preparedrawer(){ 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     navigationView = (NavigationView)findViewById(R.id.nav_view); 
     expListView = (ExpandableListView)findViewById(R.id.navList); 
     drawer = (DrawerLayout)findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 


     setprofileimage(); 
     prepareListData(); 

     listAdapter = new ExpandableListAdapter(
       this, listDataHeader,groupImages,listDataChild,drawer); 
     expListView.setAdapter(listAdapter); 

     expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { 
      @Override 
      public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) { 
       return false; 
      } 
     }); 

     expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { 

      @Override 
      public void onGroupExpand(int groupPosition) { 



       // mDrawerLayout.closeDrawers(); 
       Toast.makeText(getApplicationContext(), 
         listDataHeader.get(groupPosition) + " Expanded", 
         Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() { 

      @Override 
      public void onGroupCollapse(int groupPosition) { 

       Toast.makeText(getApplicationContext(), 
         listDataHeader.get(groupPosition) + " Collapsed", 
         Toast.LENGTH_SHORT).show(); 

      } 
     }); 

     expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 
      View _lastColored; 

      @Override 
      public boolean onChildClick(ExpandableListView parent, View v, 
             int groupPosition, int childPosition, long id) { 
       drawer.closeDrawer(GravityCompat.START); 
       // TODO Auto-generated method stub 



       Toast.makeText(
         getApplicationContext(), 
         listDataHeader.get(groupPosition) 
           + " : " 
           + listDataChild.get(
           listDataHeader.get(groupPosition)).get(
           childPosition), Toast.LENGTH_SHORT) 
         .show(); 


       return false; 
      } 
     }); 
    } 
    public void setviewstub(int layout_id){ 
     viewStub=(ViewStub)findViewById(R.id.view_stub); 
     viewStub.setLayoutResource(layout_id); 
     viewStub.inflate(); 


    } 
    public void setprofileimage(){ 

     CircleImageView profilePictureView = (CircleImageView) navigationView.getHeaderView(0).findViewById(R.id.profile_image); 
     TextView Usernamenav=(TextView)navigationView.getHeaderView(0).findViewById(R.id.Usernamenav); 

     final Userloginsession userloginsession = new Userloginsession(getApplicationContext()); 
     final HashMap<String, String> userlist = userloginsession. isGetuserDetails(); 
     String first_namee=userlist.get(Userloginsession.IS_FIRST_NAME); 
     String last_namee=userlist.get(Userloginsession.IS_LAST_NAME); 
     Picasso.with(BaseActivity.this) 
       .load(Url.imgurl + userlist.get(Userloginsession.IS_IMAGE)) 
       .fit().centerCrop() 
       .into(profilePictureView); 
     Usernamenav.setText(first_namee+" "+last_namee); 
     Log.i("imgurl",Url.imgurl + userlist.get(Userloginsession.IS_IMAGE)); 


    } 
    private void prepareListData() { 

     groupImages= new ArrayList<Integer>(); 
     groupImages.add(R.drawable.dashboard_icon); 
     groupImages.add(R.drawable.crm_icon); 
     groupImages.add(R.drawable.accouns_icon); 
     groupImages.add(R.drawable.settings_icon); 
     groupImages.add(R.drawable.logout_icon); 



     listDataHeader = new ArrayList<String>(); 
     listDataChild = new HashMap<String, List<String>>(); 

     // Adding child data 
     listDataHeader.add("Dashboard"); 
     listDataHeader.add("CRM"); 
     listDataHeader.add("Accounts"); 
     listDataHeader.add("Settings"); 
     listDataHeader.add("LogOut"); 

     // Adding child data 

     List<String> Dashboard = new ArrayList<String>(); 

     List<String> CRM = new ArrayList<String>(); 
     CRM.add("Today Followup's"); 
     CRM.add("Unhandled Followup(s)"); 
     CRM.add("Today Appointments"); 
     CRM.add("Unhandled Appointment(s)"); 
     CRM.add("Cold Call(s)"); 


     List<String> Accounts = new ArrayList<String>(); 
     Accounts.add("Quotations"); 
     Accounts.add("Orders"); 
     Accounts.add("Payments"); 


     List<String> Settings = new ArrayList<String>(); 

     List<String> Logout = new ArrayList<String>(); 
     listDataChild.put(listDataHeader.get(0), Dashboard); 
     listDataChild.put(listDataHeader.get(1), CRM); // Header, Child data 
     listDataChild.put(listDataHeader.get(2), Accounts); 
     listDataChild.put(listDataHeader.get(3), Settings); 
     listDataChild.put(listDataHeader.get(4), Logout); 

    } 
public static void dointent(Context A1, Class A2){ 

    Intent i = new Intent().setClass(A1, A2); 
    i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT|Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_NEW_TASK); 
    A1.startActivity(i); 


} 
    public static Typeface settypefacess(Context _context,String fonts){ 

     Typeface roboto = Typeface.createFromAsset(_context.getAssets(), 
       fonts); 
     return roboto; 
    } 

} 

我的適配器:`

public class ExpandableListAdapter extends BaseExpandableListAdapter { 
    private Context _context; 
    private List<String> _listDataHeader; // header titles 
    // child data in format of header title, child title 
    private List<Integer> _group; 

    private DrawerLayout _drawer; 

    private static final int[] EMPTY_STATE_SET = {}; 
    private static final int[] GROUP_EXPANDED_STATE_SET = 
      {android.R.attr.state_expanded}; 
    private static final int[][] GROUP_STATE_SETS = { 
      EMPTY_STATE_SET, // 0 
      GROUP_EXPANDED_STATE_SET // 1 
    }; 

    private HashMap<String, List<String>> _listDataChild; 
    ExpandableListView expandList; 
    public ExpandableListAdapter(Context context, List<String> listDataHeader, List<Integer> group, 
           HashMap<String, List<String>> listChildData, DrawerLayout drawer) { 
     this._context = context; 
     this._listDataHeader = listDataHeader; 
     this._listDataChild = listChildData; 
     this._group=group; 
     this._drawer=drawer; 

    } 

    @Override 
    public Object getChild(int groupPosition, int childPosititon) { 
     return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
       .get(childPosititon); 
    } 
    @Override 
    public long getChildId(int groupPosition, int childPosition) { 
     return childPosition; 
    } 

    @Override 
    public View getChildView(final int groupPosition, final int childPosition, 
          boolean isLastChild, View convertView, ViewGroup parent) { 

     final String childText = (String) getChild(groupPosition, childPosition); 
     int childpositionaa= (int) getChildId(groupPosition,childPosition); 
     int gropuposi= (int) getGroupId(groupPosition); 
     Log.i("groupposi", String.valueOf(+groupPosition)); 
     Log.i("groupposicc", String.valueOf(+childPosition)); 
     Log.i("groupposiccee", String.valueOf(+childpositionaa)); 
     int child3position=(int)getChildId(1,childPosition); 
     Log.i("groupposiccee33", String.valueOf(+child3position)); 

     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) this._context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.list_item, null); 
     } 
Log.i("childposition",""+childPosition); 
     Typeface roboto = Typeface.createFromAsset(_context.getAssets(), 
       "font/Roboto-Light.ttf"); 
     AppCompatTextView txtListChild = (AppCompatTextView) convertView 
       .findViewById(R.id.lblListItem); 
     txtListChild.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View view, MotionEvent motionEvent) { 
       switch (motionEvent.getAction()) { 
        case MotionEvent.ACTION_DOWN: 

         _drawer.closeDrawer(GravityCompat.START); 
         switch (groupPosition) { 
          case 0: 
           break; 
          case 1: 
           switch (childPosition){ 
            case 0: 
             BaseActivity.dointent(_context.getApplicationContext(),Handled_follow.class); 

             break; 
            case 1: 
             BaseActivity.dointent(_context.getApplicationContext(), Unhanded_follow.class); 
             break; 
            case 2: 
             BaseActivity.dointent(_context.getApplicationContext(), Navi_recyview.class); 
               break; 
            case 3: 
             BaseActivity.dointent(_context.getApplicationContext(), Unhandled_appoint.class); 
                      break; 
            case 4: 
             BaseActivity.dointent(_context.getApplicationContext(), Cold_calls.class); 
                       break; 
           } 
           break; 
         }       
         break; 
       } 

       return false; 
      } 
     }); 
     AppCompatTextView txtchildcount= (AppCompatTextView) convertView 
       .findViewById(R.id.lblistnavcount); 
     txtListChild.setText(childText); 
     txtListChild.setTypeface(roboto); 


     txtchildcount.setText(myList.get(childpositionaa)); 
     if(gropuposi==2){ 
      if(childpositionaa==0){ 
       txtchildcount.setText(myList.get(5)); 

      } 
      else{ 
       txtchildcount.setText(myList.get(4)); 
      } 
      txtchildcount.setText(myList.get(childpositionaa+5)); 
     } 
     else{ 
      txtchildcount.setText(myList.get(childpositionaa)); 
     } 
     return convertView; 
    } 

    @Override 
    public int getChildrenCount(int groupPosition) { 
     return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
       .size(); 
    } 

    @Override 
    public Object getGroup(int groupPosition) { 
     return this._listDataHeader.get(groupPosition); 
    } 

    @Override 
    public int getGroupCount() { 
     return this._listDataHeader.size(); 
    } 

    @Override 
    public long getGroupId(int groupPosition) { 
     return groupPosition; 
    } 

    @Override 
    public View getGroupView(final int groupPosition, boolean isExpanded, 
          View convertView, ViewGroup parent) { 
     String headerTitle = (String) getGroup(groupPosition); 
     int headerimg = _group.get(groupPosition); 
     Log.i("groupposigroupview", String.valueOf(+groupPosition)); 
     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) this._context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.list_group, null); 
     } 
     AppCompatTextView lblListHeader = (AppCompatTextView) convertView 
       .findViewById(R.id.lblListHeader); 
     View ind = convertView.findViewById(R.id.indicicon); 

     if(ind != null) { 
      ImageView indicator = (ImageView)ind; 
      if(getChildrenCount(groupPosition) == 0) { 
       indicator.setVisibility(View.INVISIBLE); 
      } else { 
       indicator.setVisibility(View.VISIBLE); 
       if (isExpanded) { 
        lblListHeader.setTextColor(_context.getResources().getColor(R.color.colorPrimary)); 
        indicator.setImageResource(R.drawable.ic_expand_less_black_18dp); 
       } else { 
        lblListHeader.setTextColor(_context.getResources().getColor(R.color.colorlisthead)); 

        indicator.setImageResource(R.drawable.ic_expand_more_black_18dp); 
       } 
      } 
     } 
     Typeface roboto = Typeface.createFromAsset(_context.getAssets(), 
       "font/Roboto-Regular.ttf"); //use this.getAssets if you are calling from an Activity 


     lblListHeader.setTypeface(roboto); 
     lblListHeader.setText(headerTitle); 
     ImageView imgsListHeader = (ImageView) convertView 
       .findViewById(R.id.imagview); 

     imgsListHeader.setImageResource(headerimg); 
lblListHeader.setOnTouchListener(new View.OnTouchListener() { 
    @Override 
    public boolean onTouch(View view, MotionEvent motionEvent) { 
     switch (motionEvent.getAction()) { 
      case MotionEvent.ACTION_DOWN: 

       switch (groupPosition) { 
        case 0: 
         _drawer.closeDrawer(GravityCompat.START); 
         BaseActivity.dointent(_context.getApplicationContext(),Dashboard.class); 
         break; 
        case 1: 

         break; 
        case 2: 

        case 3: 
         _drawer.closeDrawer(GravityCompat.START); 

         break; 
        case 4: 
         _drawer.closeDrawer(GravityCompat.START); 

         break; 
       } 
       break; 
     } 

     return false; 
    } 
}); 
     return convertView; 
    } 

    @Override 
    public boolean hasStableIds() { 
     return false; 
    } 
    @Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) { 


     return true; 
    } 

} 
` 

回答

0

你需要的是修改頁眉和GroupItem相關的層次結構中的當前位置。

爲了做到這一點,您的ListAdapter需要知道這個位置是什麼。例如,您可以將其作爲參數傳遞給構造函數。

然後,在對組頭(getGroupView)和組項目(getChildView)進行充氣時,如果項目是「當前位置」,則將textview裝飾設置爲粗體(或爲選定項目膨脹另一個特定佈局)。

標題和子元素的位置作爲getGroupView和getChildView方法的參數傳遞。

請看that postthat one回答有關您的問題。

+0

@thank you w00ly ...在上圖中看到如果我選擇儀表板,我希望文本變成藍色,直到我選擇另一個列表。如果我選​​擇冷電話,然後我想要CRM和冷呼叫文本爲藍色和其他人是黑色的 – lucky

+0

我想你的所有活動都是從BaseActivity擴展而來的。 那麼,當你點擊你的「冷調用」時,你正在啓動一個意圖,對吧?在這裏,您將所選項目作爲參數傳遞給您的班級(您的班級)。你也可以傳遞一個id,當刷新抽屜時,使用這個參數來知道你是哪一節,並在你的listAdapter中做相應的修改。 – w00ly

相關問題