我制作了一个标签,其中我使用了gridview来显示图像,textviews
...我想让听众...当用户点击gridview
中的照片时...它转移到下一个活动,即image_description活动。
这是活动代码。
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class HomeFragment extends Fragment implements AdapterView.OnItemClickListener {
public HomeFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
View vew= inflater.inflate(R.layout.activity_art_dic,container,false);
final GridView gridview = (GridView)view.findViewById(R.id.gridview);
gridview.setOnItemClickListener(this);
gridview.setDrawSelectorOnTop(true);
List<ItemObject> allItems = getAllItemObject();
CustomAdapter customAdapter = new CustomAdapter(getActivity(), allItems);
gridview.setAdapter(customAdapter);
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(), "Position: " + position, Toast.LENGTH_SHORT).show();
}
});
return view;
}
private List<ItemObject> getAllItemObject(){
List<ItemObject> items = new ArrayList<>();
items.add(new ItemObject(R.drawable.painting,"Dip It Low", "Christina Milian"));
items.add(new ItemObject(R.drawable.paintingo,"Someone like you", "Adele Adkins"));
items.add(new ItemObject(R.drawable.drawing,"Ride", "Ciara"));
items.add(new ItemObject(R.drawable.sketchbook,"Paparazzi", "Lady Gaga"));
items.add(new ItemObject(R.drawable.drawing,"Forever", "Chris Brown"));
items.add(new ItemObject(R.drawable.paintingo,"Stay", "Rihanna"));
items.add(new ItemObject(R.drawable.sketchbook,"Marry me", "Jason Derulo"));
items.add(new ItemObject(R.drawable.painting,"Waka Waka", "Shakira"));
items.add(new ItemObject(R.drawable.drawing,"Dark Horse", "Katy Perry"));
items.add(new ItemObject(R.drawable.paintingo,"Dip It Low", "Christina Milian"));
items.add(new ItemObject(R.drawable.painting,"Dip It Low", "Christina Milian"));
items.add(new ItemObject(R.drawable.sketchbook,"Dip It Low", "Christina Milian"));
return items;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ItemObject image= (ItemObject) parent.getItemAtPosition(position);
Intent intent=new Intent(getActivity(),photo_description.class);
startActivity(intent);
}
}
以下是它的样子
答案 0 :(得分:1)
这是因为你设置了多个itemClickListeners。 从onCreateView中删除以下代码。
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(), "Position: " + position, Toast.LENGTH_SHORT).show();
}
});
答案 1 :(得分:0)
onItemClicklistner无效
因为为同一个GridView设置了onItemClicklistner
两次,所以onItemClicklistner
的最后一次调用将起作用并在点击GridView项目时显示Toast消息。
要点击项目移至下一个活动,请移除setOnItemClickListener
方法
答案 2 :(得分:0)
从您的代码中移除此部分。
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(), "Position: " + position, Toast.LENGTH_SHORT).show();
}
});
因为这是你已经定义的并且已经两次。
gridview.setOnItemClickListener(this);
然后添加
onClickListener
方法并执行您想要的操作。
@Override
public void onClick(View v) {
//do what you want to do when button is clicked
switch (v.getId()) {
case R.id.gridview:
// Switch Activity here
break;
case default:
break;
}
}