2017-01-02 126 views
0

我试图在网格视图内部实现卡片视图。网格视图显示完美;即使当我触摸一个网格视图项目时,它也不会显示任何内容。但是当我点击显示在其下方的文字时。我提到This但不工作...GridView onClickListener/onItemClickListener无法正常工作

谢谢。

我的代码是如下,

// Contact_Backup.java

public class Contact_Backup extends AppCompatActivity { 

     @Bind(R.id.gridview) 
     GridView gridview; 
     String[] contact_screen_array; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.contact_backup); 
      ButterKnife.bind(this); 
      contact_screen_array = getResources().getStringArray(R.array.TextScreen); 
      String[] contact_icon_array = getResources().getStringArray(R.array.IconScreen); 
      String concat = "\nContacts"; 
      gridview.setAdapter(new Custom_Adapter(getApplicationContext(),contact_icon_array,contact_screen_array,concat)); 
     } 
    } 

// Custom_Adapter

public class Custom_Adapter extends BaseAdapter { 

    Context contaxt; 
    String[] Array_text; 
    String[] Array_icon; 
    String concat; 
    LayoutInflater inflater; 

    public Custom_Adapter(Context context,String[] Array_icon, String[] Array_text, String concat) 
    { 
     this.contaxt = context; 
     this.Array_text = Array_text; 
     this.Array_icon = Array_icon; 
     this.concat=concat; 
     inflater = (LayoutInflater)context. 
       getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 
    @Override 
    public int getCount() { 
     return Array_text.length; 
    } 
    @Override 
    public Object getItem(int position) { 
     return position; 
    } 
    @Override 
    public long getItemId(int position) { 
     return position; 
    } 


    @Bind(R.id.Screen_icon) 
    Button Screen_icon; 
    @Bind(R.id.Screen_text) 
    TextView Screen_text; 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     View grid; 
     LayoutInflater inflater = (LayoutInflater) contaxt 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     if (convertView == null) { 
      grid = new View(contaxt); 
      grid = inflater.inflate(R.layout.new_app_card_layout, null); 
      ButterKnife.bind(this, grid); 
      Screen_text.setText(Array_text[position]+concat); 
      Screen_icon.setText(Array_icon[position]); 
      Typeface font = Typeface.createFromAsset(contaxt.getAssets(), "fonts/fontawesome-webfont.ttf"); 
      Screen_icon.setTypeface(font); 
     } else { 
      grid = (View) convertView; 
     } 

     grid.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Log.d("TAG::",""+position); 
      } 
     }); 
     return grid; 
    } 
} 

// contact_backup.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/main_backgorund" 
    > 
    <RelativeLayout 
     android:background="@color/transparent" 
     android:padding="5.0dip" 
     android:id="@+id/top_bar_screen" 
     android:layout_weight="0.2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
     <TextView 
      android:textColor="@color/newclr" 
      android:id="@+id/tvContacts" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/contacts" 
      android:shadowColor="#000000" 
      android:shadowDx="1.0" 
      android:shadowDy="1.0" 
      android:shadowRadius="2.0" 
      android:layout_centerVertical="true" /> 
     <TextView 
      android:textColor="@color/newclr" 
      android:id="@+id/tvLastBackup" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/last_backup" 
      android:shadowColor="#000000" 
      android:shadowDx="1.0" 
      android:shadowDy="1.0" 
      android:shadowRadius="2.0" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" /> 
    </RelativeLayout> 

    <View 
     android:layout_width="fill_parent" 
     android:layout_height="1dp" 
     android:id="@+id/v1" 
     android:layout_below="@+id/top_bar_screen" 
     android:background="@color/newclr"/> 

    <GridLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/swipe" 
     android:layout_below="@+id/v1" 
     android:paddingTop="20sp" 
     android:paddingLeft="10sp" 
     android:paddingRight="10sp" 
     android:layout_weight="0.6" 
     > 

    <GridView 
     android:id="@+id/gridview" 
     android:layout_width="fill_parent" 
     android:columnWidth="90dp" 
     android:numColumns="auto_fit" 
     android:verticalSpacing="10dp" 
     android:horizontalSpacing="10dp" 
     android:layout_height="wrap_content" /> 
    </GridLayout> 

    <!-- com.startapp.android.publish.banner.Banner 
     android:id="@+id/startAppBanner1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_weight="0.2" 
     android:layout_centerHorizontal="true" /--> 


</RelativeLayout> 

// new_app_card_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/fssr_card" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    card_view:cardBackgroundColor="@color/transparent" 
    > 

    <LinearLayout 
     android:gravity="center" 
     android:orientation="vertical" 
     android:id="@+id/Contacts_Button_Layout" 
     android:background="@drawable/linearlayout_normal" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_marginLeft="-1.0dip" 
     android:layout_weight="1.0" 
     > 

     <Button 
      android:textSize="35.0sp" 
      android:textColor="@color/newclr" 
      android:layout_gravity="center" 
      android:id="@+id/Screen_icon" 
      android:background="@null" 
      android:padding="20sp" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/fa_icon_contact" 
      android:drawablePadding="5.0dip" 
      android:textAllCaps="false" /> 

     <TextView 
      android:textSize="15.0sp" 
      android:textColor="@color/white" 
      android:layout_gravity="center" 
      android:textAlignment="center" 
      android:id="@+id/Screen_text" 
      android:background="@color/newclr" 
      android:padding="5sp" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/contactsbackup" 
      android:textAllCaps="false"/> 

    </LinearLayout> 
</android.support.v7.widget.CardView> 

回答

0

将侦听器设置为实际的gridview对象。 gridView.setOnItemClickListener(new OnItemClickListener()...);

+0

我尝试在Contact_Backup.java类,但这些代码并没有影响。 –

+0

非常感谢你的几次:) :) –

0

试试这个,

public class Contact_Backup extends AppCompatActivity { 

     @Bind(R.id.gridview) 
     GridView gridview; 
     String[] contact_screen_array; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.contact_backup); 
      ButterKnife.bind(this); 
      contact_screen_array = getResources().getStringArray(R.array.TextScreen); 
      String[] contact_icon_array = getResources().getStringArray(R.array.IconScreen); 
      String concat = "\nContacts"; 
      gridview.setAdapter(new Custom_Adapter(getApplicationContext(),contact_icon_array,contact_screen_array,concat)); 
      gridview.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        for(int i=0;i<contact_screen_array.length;i++) { 
         Log.d("TAG::", "Position"+contact_screen_array[i]); 
        } 
       } 
      }); 
     } 
    } 
+0

从适配器类中删除监听器,并在活动中使用它 –

+0

我试过了,但它不工作的朋友......它只在点击contact_screen_array Textview时才起作用。 .. –

+0

非常感谢你的几个时间.... :) –

-1

试试这个我已经更新您的定义适配器 class.please这个更新的代码再次检查。

public class Custom_Adapter extends BaseAdapter { 

    Context contaxt; 
    String[] Array_text; 
    String[] Array_icon; 
    String concat; 
    LayoutInflater inflater; 

    public Custom_Adapter(Context context,String[] Array_icon, String[] Array_text, String concat) 
    { 
     this.contaxt = context; 
     this.Array_text = Array_text; 
     this.Array_icon = Array_icon; 
     this.concat=concat; 
     inflater = (LayoutInflater)context. 
       getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 
    @Override 
    public int getCount() { 
     return Array_text.length; 
    } 
    @Override 
    public Object getItem(int position) { 
     return position; 
    } 
    @Override 
    public long getItemId(int position) { 
     return position; 
    } 


    @Bind(R.id.Screen_icon) 
    Button Screen_icon; 
    @Bind(R.id.Screen_text) 
    TextView Screen_text; 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     View grid; 
     LayoutInflater inflater = (LayoutInflater) contaxt 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     if (convertView == null) { 
      grid = new View(contaxt); 
      grid = inflater.inflate(R.layout.new_app_card_layout, null); 
      ButterKnife.bind(this, grid); 
      Screen_text.setText(Array_text[position]+concat); 
      Screen_icon.setText(Array_icon[position]); 
      Typeface font = Typeface.createFromAsset(contaxt.getAssets(), "fonts/fontawesome-webfont.ttf"); 
      Screen_icon.setTypeface(font); 

      grid.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Log.d("TAG::",""+position); 
       } 
      }); 

     } else { 
      grid = (View) convertView; 

      grid.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Log.d("TAG::",""+position); 
       } 
      }); 
     } 
     return grid; 
    } 
} 

希望这可以帮助你!

+0

我试过了,但它不工作的朋友...同样的问题发生时,它只有当我点击contact_screen_array Textview ....时才工作。 –

+1

你想要与“android:id =”@ + id/Screen_icon“”这个按钮相同? –

+0

是的。可能它不工作仅仅是因为“android:id =”@ + id/Screen_icon“是Button? –

0

使用这种方法为点击一个gridview的项目:

gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

     // your action when gridView item is clicked 

     } 
    }); 
+0

不工作... :( –

+0

@ JigneshM.Mehta你是什么意思不工作?你想在单元格点击后做什么?请确保你在gridview存在的活动中调用onITEMclicklistener。 – babak

+0

buddy,thanks for我的问题是我在Custom Grid中使用了Button,当我点击一个Grid时,Grid的代码被按钮点击覆盖,这样就不会像我想要的那样工作。 Textview,而不是按钮。然后,我会很好地工作。顺便说一句,谢谢你的帮助。你的代码现在也工作正常.. :) –

0

最后,我成功了。这里是代码。

只需在// new_app_card_layout.xml中使用Textview而不是Button即可。

只更新它,

<TextView 
     android:textSize="35.0sp" 
     android:textColor="@color/newclr" 
     android:layout_gravity="center" 
     android:id="@+id/Screen_icon" 
     android:background="@null" 
     android:padding="20sp" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/fa_icon_contact" 
     android:drawablePadding="5.0dip" 
     android:textAllCaps="false" 
     android:textAlignment="center" /> 

太感谢你了,所有的你的帮助... :)