从活动的片段的listview的自定义基本适配器按钮单击到另一个活动的共享转换不起作用

时间:2018-05-07 16:35:40

标签: android shared-element-transition

我使用listview和自定义BaseAdapter来显示各种项目。列表视图位于FragmentA内部,FragmentA使用MainActivity的视图寻呼机显示。现在我想要从自定义适配器按钮onclick到Main2Activity的共享元素转换。我已经在SO中搜索了很多并尝试过,但没有一个能解决我的问题。当我单击自定义适配器内的按钮时,Main2Activity根本不显示,但在那一刻,如果我旋转我的移动屏幕然后显示它。我已经非常仔细地检查了我的代码,但却找不到它的错误。所以请在这方面帮助我。

public class MySBaseAdapter extends BaseAdapter {
private Context mContext;
private List<Map<String,Object>> data=new ArrayList<Map<String, Object>>();
public LayoutInflater inflater=null;  
String itemNo, itemId;
Activity activity;
public MySBaseAdapter(Context context, List<Map<String, Object>> data, Activity thisactitity, String itemno, String itemid){
    mContext=context;
    this.data=data;
    this.itemNo=itemno;
    this.itemId=itemid;
    activity=thisactitity;
    inflater=(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, final View convertView, ViewGroup parent) {
item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imvb.buildDrawingCache();
                Bitmap bmp=imvb.getDrawingCache();
                Intent intent = new Intent(mContext, Main2Activity.class);
                intent.putExtra("bitmap_data",bmp);                    intent.putExtra("item_name",itemname.getText().toString());                   
                intent.putExtra("item_num",itemNo);
                intent.putExtra("item_id",itemId);                   
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                Pair<View, String> pair1 = Pair.create((View)imvb,"phototrans");
                ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, pair1);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    mContext.startActivity(intent, options.toBundle());
                }else {
                    mContext.startActivity(intent);
                }
            }
        });

自定义适配器类

public class MySBaseAdapter extends BaseAdapter {
private Context mContext;
private List<Map<String,Object>> data=new ArrayList<Map<String, Object>>();
public LayoutInflater inflater=null;  
String itemNo, itemId;
Activity activity;
public MySBaseAdapter(Context context, List<Map<String, Object>> data, Activity thisactitity, String itemno, String itemid){
    mContext=context;
    this.data=data;
    this.itemNo=itemno;
    this.itemId=itemid;
    activity=thisactitity;
    inflater=(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, final View convertView, ViewGroup parent) {
item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imvb.buildDrawingCache();
                Bitmap bmp=imvb.getDrawingCache();
                Intent intent = new Intent(mContext, Main2Activity.class);
                intent.putExtra("bitmap_data",bmp);                    intent.putExtra("item_name",itemname.getText().toString());                   
                intent.putExtra("item_num",itemNo);
                intent.putExtra("item_id",itemId);                   
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                Pair<View, String> pair1 = Pair.create((View)imvb,"phototrans");
                ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, pair1);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    mContext.startActivity(intent, options.toBundle());
                }else {
                    mContext.startActivity(intent);
                }
            }
        });

Main2Activity

public class Main2Activity extends AppCompatActivity {
Context pContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
    getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main31);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){
        prorileimage.setTransitionName("phototrans");
    }

1 个答案:

答案 0 :(得分:0)

什么是项目,是按钮还是其他视图?如果您的非按钮已在xml文件中设置可点击属性 true ?您可以在单击侦听器中打印日志。

我关注的另一件事是this文件说,过渡决定了观点如何在活动或片段之间转换;你已经采取了位图,所以它会转换或不转换。我不确定:

共享元素转换确定两个片段中存在的视图如何在它们之间转换。例如,当B变为可见时,片段A和片段B上的ImageView上显示的图像从A转换为B.