Android从Textview过渡到Edittext

时间:2015-05-27 18:17:01

标签: java android android-recyclerview android-transitions

我的Android应用程序中有这个奇怪的错误。我有一个带有回收者视图的活动和一些项目(在我的情况下,学校科目有四个textViews和一个framelayout)。 单击某个项目时,将执行包含共享元素的转换,并输入EditActivity,您可以在其中编辑主题。转换完成后,editText中的文本向上移动。只有当您单击editText时,文本才会正常,我不知道为什么。有时文本显得正常。也许是因为我从textView(在项目中)转换为editText(在EditActitvty中)。 请指教。这是我的代码。

使用RecyclerView

SubjectActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    if (Learn.sdk21()) {
        //To enable window content transitions in your code instead, call the Window.requestFeature() method:
        getWindow().requestFeature(android.view.Window.FEATURE_CONTENT_TRANSITIONS);
        Transition ts_enter = new Explode();   //Slide(); //Explode();
        Transition ts_exit = new ChangeBounds();

        ts_enter.setDuration(Learn.animationTime);
        ts_exit.setDuration(Learn.animationTime);
    /*
    If you have set an enter transition for the second activity,
    the transition is also activated when the activity starts.
    */
        getWindow().setEnterTransition(ts_enter);
        getWindow().setExitTransition(ts_exit);
    }
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_subject);
    //Database
    dao = new DAO(getApplicationContext());
    subjectList = dao.getAllSubjectsAlphabetical();

    //Custom Toolbar
    toolbar = (Toolbar) findViewById(R.id.toolbar_main);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    toolbar.setTitle(R.string.Subject);
    recyclerViewSubject = (RecyclerView) findViewById(R.id.recyclerViewSubject);
    setRecyclerViewSubject();


    //FloatingActionButton
    fabAddSubject = (FloatingActionButton)
            findViewById(R.id.fabAddSubject);
    fabAddSubject.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(getApplicationContext(), AddSubjectActivity.class));
        }
    });
    fabAddSubject.attachToRecyclerView(recyclerViewSubject);


}

private void setRecyclerViewSubject() {
    //RecyclerView

    subjectRecyclerViewAdapter =
            new SubjectRecyclerViewAdapter(this, dao.getAllSubjectsAlphabetical());
    subjectRecyclerViewAdapter.setClickListener(this);
    recyclerViewSubject.setLayoutManager(new LinearLayoutManager(this));
    recyclerViewSubject.setAdapter(new ScaleInAnimationAdapter(subjectRecyclerViewAdapter));

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_subject, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {

        return true;
    } else if (id == android.R.id.home) {
        this.finish();
        return true;
    } else if (id == R.id.debugDelete) {

        try {
            deleteAll();
        } catch (Exception e) {
            System.out.println("--> Tried to delete all but: " + e + "<--");
        }

        return true;
    } else if (id == R.id.debugAdd) {

        try {
            add();

        } catch (Exception e) {
            System.out.println("--> Tried to delete add5: " + e + "<--");
        }
        return true;
    } else if (id == R.id.refreshSubjectList) {
        subjectList = dao.getAllSubjectsAlphabetical();
        subjectRecyclerViewAdapter.dataChange(subjectList);

    }

    return super.onOptionsItemSelected(item);
}

@Override
public void itemClicked(View v, int postion) {
    editSubject(v, postion);
}

private void editSubject(View v, int pos) {
    Intent intent = new Intent(getApplicationContext(), EditSubjectActivity.class);
    intent.putExtra("subjectPos", pos);


    if (Learn.sdk21()) {
        View view = findViewById(R.id.buttonColor);

        Pair<View, String> p1 = Pair.create(v.findViewById(R.id.frameLayoutSubjectColor),
                v.findViewById(R.id.frameLayoutSubjectColor).getTransitionName());
        Pair<View, String> p2 = Pair.create(v.findViewById(R.id.textViewSubject),
                v.findViewById(R.id.textViewSubject).getTransitionName());
        Pair<View, String> p3 = Pair.create(v.findViewById(R.id.textViewShort),
                v.findViewById(R.id.textViewShort).getTransitionName());


        Pair<View, String> p4 = Pair.create(v.findViewById(R.id.textViewRoom),
                v.findViewById(R.id.textViewRoom).getTransitionName());
        Pair<View, String> p5 = Pair.create(v.findViewById(R.id.textViewTeacher),
                v.findViewById(R.id.textViewTeacher).getTransitionName());

        Pair<View, String> p6 = Pair.create(findViewById(R.id.fabAddSubject),
                findViewById(R.id.fabAddSubject).getTransitionName());

        ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(SubjectActivity.this,
                p1, p2, p3, p4, p5, p6);

        intent.putExtra("SubjectColor", v.findViewById(R.id.frameLayoutSubjectColor).getTransitionName());
        intent.putExtra("SubjectName", v.findViewById(R.id.textViewSubject).getTransitionName());
        intent.putExtra("SubjectShort", v.findViewById(R.id.textViewShort).getTransitionName());
        intent.putExtra("SubjectRoom", v.findViewById(R.id.textViewRoom).getTransitionName());
        intent.putExtra("SubjectTeacher", v.findViewById(R.id.textViewTeacher).getTransitionName());

        startActivityForResult(intent, 12, options.toBundle());

    } else {
        startActivityForResult(intent, 12);
    }

}

private void add() {
    String[] name = {"Deutsch", "Englisch", "Biologie", "Mathe", "Geschichte",
            "Musik", "Physik", "Politik", "Informatik", "Religion"};
    String[] nameshort = {"Deu", "Eng", "Bio", "Mat", "Gesch",
            "Mus", "Phy", "PoWi", "Info", "Reli"};
    String[] room = {"A222", "T292", "B322", "M22", "G666",
            "A373", "B888", "C938", "I999", "T666"};
    String[] t = {"Test1", "Test2", "Test3", "Test4", "Test5",
            "Test6", "Test7", "Test8", "Test9", "Test10"};

    String[] c = {"#fff200", "#ff0000", "#ff9000", "#00bbff", "#2aff00",
            "#0037ff", "#90ff00", "#00ff4c", "#00ffb2", "#4c00ff"};


    for (int i = 0; i < name.length; i++) {
        Subject subject = new Subject(name[i], nameshort[i], room[i], t[i], Color.parseColor(c[i]));
        dao.addSubject(subject);
    }
    setRecyclerViewSubject();

}

private void deleteAll() {
    int oldSize = dao.getAllSubjects().size();
    for (int i = (oldSize - 1); i >= 0; i--) {
        int id = dao.getAllSubjects().get(i).getId();
        dao.deleteSubject(id);
    }
    subjectRecyclerViewAdapter =
            new SubjectRecyclerViewAdapter(getApplicationContext(), dao.getAllSubjectsAlphabetical());
    recyclerViewSubject.setAdapter(new ScaleInAnimationAdapter(subjectRecyclerViewAdapter));
    recyclerViewSubject.setLayoutManager(new LinearLayoutManager(this));
    subjectRecyclerViewAdapter.notifyItemRangeRemoved(0, dao.getAllSubjects().size());
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    int subId = data.getIntExtra("subjectId", 0);
    int subPos = data.getIntExtra("subjectPos", 0);
    int subNewPos = data.getIntExtra("subjectNewPos", subPos);
    subjectList = dao.getAllSubjectsAlphabetical();

    System.out.print("requestCode" + requestCode
            + "\n" +
            "resultCode" + resultCode
            + "\n" +
            "subId" + subId
            + "\n" +
            "subPos" + subPos
            + "\n" +
            "subNewPos" + subNewPos);
    if (requestCode == 12) {
        if (resultCode == 2) {//Edit
            subjectRecyclerViewAdapter.updateItem(subjectList, subPos, subNewPos);
            recyclerViewSubject.setAdapter(subjectRecyclerViewAdapter);
        } else if (resultCode == 3) {//Delete
            subjectRecyclerViewAdapter.removeItem(subjectList, subPos);
            recyclerViewSubject.setAdapter(subjectRecyclerViewAdapter);
        }
    }

  }
}

EditSubjectActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    if (Learn.sdk21()) {
        //To enable window content transitions in your code instead, call the Window.requestFeature() method:
        getWindow().requestFeature(android.view.Window.FEATURE_CONTENT_TRANSITIONS);
        Transition ts_enter = new Explode(); //Slide(); //Explode();
        Transition ts_exit = new ChangeBounds(); //Slide(); //Explode();

        ts_enter.setDuration(Learn.animationTime);
        ts_exit.setDuration(Learn.animationTime);

        getWindow().setEnterTransition(ts_enter);
        getWindow().setExitTransition(ts_exit);
    }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edit_subject);
    //Database
    dao = new DAO(getApplicationContext());

    //Custom Toolbar
    toolbar = (Toolbar) findViewById(R.id.toolbar_main);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    buttonColor = (Button) findViewById(R.id.buttonColor);
    buttonColor.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            colorPickerDialog = new ColorPickerDialog(EditSubjectActivity.this, subject.getColor());
            colorPickerDialog.setAlphaSliderVisible(false);
            colorPickerDialog.setHexValueEnabled(true);
            colorPickerDialog.setTitle("Background");
            colorPickerDialog.setOnColorChangedListener(new ColorPickerDialog.OnColorChangedListener() {
                @Override
                public void onColorChanged(int i) {
                    subject.setColor(i);
                    buttonColor.setText("#" + Integer.toHexString(subject.getColor()));
                    buttonColor.setBackgroundColor(i);
                }
            });
            colorPickerDialog.show();
        }
    });

    //EditText
    editTextSubject = (EditText) findViewById(R.id.editTextSubject);
    editTextShort = (EditText) findViewById(R.id.editTextShort);
    editTextRoom = (EditText) findViewById(R.id.editTextRoom);
    editTextTeacher = (EditText) findViewById(R.id.editTextTeacher);

    //FAB
    fabSaveSubject = (FloatingActionButton) findViewById(R.id.fabSaveSubject);
    fabSaveSubject.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            saveSubject();

        }
    });
    fabDeleteSubject = (FloatingActionButton) findViewById(R.id.fabDeleteSubject);
    fabDeleteSubject.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            deleteSubject();
        }
    });
    setUp();

    if(Learn.sdk21()){
        Intent intent = getIntent();
        editTextSubject.setTransitionName(intent.getStringExtra("SubjectName"));
        editTextShort.setTransitionName(intent.getStringExtra("SubjectShort"));
        editTextRoom.setTransitionName(intent.getStringExtra("SubjectRoom"));
        editTextTeacher.setTransitionName(intent.getStringExtra("SubjectTeacher"));
        buttonColor.setTransitionName(intent.getStringExtra("SubjectColor"));
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_add_subject, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    } else if (id == android.R.id.home) {
        finish(1);
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    finish(1);
}

private void saveSubject() {
    subject.setSubject(editTextSubject.getText().toString());
    subject.setSubjectShort(editTextShort.getText().toString());
    subject.setRoom(editTextRoom.getText().toString());
    subject.setTeacher(editTextTeacher.getText().toString());
    dao.updateSubject(subjectId, subject);
    finish(2);
}

private void deleteSubject() {
    dao.deleteSubject(subjectId);
    finish(3);
}


private void setUp() {
    Intent intent = getIntent();
    subjectPos = intent.getIntExtra("subjectPos", 0);
    subject = dao.getAllSubjectsAlphabetical().get(subjectPos);
    subjectId = subject.getId();
    editTextSubject.setText(subject.getSubject());
    editTextShort.setText(subject.getSubjectShort());
    editTextRoom.setText(subject.getRoom());
    editTextTeacher.setText(subject.getTeacher());
    buttonColor.setText("#" + Integer.toHexString(subject.getColor()));
    buttonColor.setBackgroundColor(subject.getColor());
}

private void finish(int i) {
    Intent intent = new Intent();
    intent.putExtra("subjectPos", subjectPos);
    intent.putExtra("subjectId", subjectId);
    setResult(i, intent);
    switch (i) {
        case 1: //Nothing
            finishAfterTransition();
            break;
        case 2: //Edit
            int newPos = dao.getSubjectPosition(dao.getAllSubjectsAlphabetical(), subject);
            intent.putExtra("subjectNewPos", newPos);
            finishAfterTransition();
            break;
        case 3: //Delete
            finish();
            break;
    }
  }
}

SubjectRecyclerViewAdapter.java:

public class SubjectRecyclerViewAdapter
    extends RecyclerView.Adapter
    <SubjectRecyclerViewAdapter.subjectViewHolder> {

LayoutInflater inflater;
List<Subject> subjectList;
Context context;
ClickListener clickListener;
DAO dao;


public SubjectRecyclerViewAdapter(Context context, List<Subject> subjectList) {
    inflater = LayoutInflater.from(context);
    this.subjectList = subjectList;
    this.context = context;
    dao = new DAO(context);
}

@Override
public subjectViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.subject_item, parent, false);
    subjectViewHolder holder = new subjectViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(subjectViewHolder holder, int position) {
    Subject subject = subjectList.get(position);
    holder.textViewSubject.setText(subject.getSubject());
    holder.textViewShort.setText(subject.getSubjectShort());
    holder.textViewTeacher.setText(subject.getTeacher());
    holder.textViewRoom.setText(subject.getRoom());
    holder.frameLayoutSubjectColor.setBackgroundColor(subject.getColor());
    if (Learn.sdk21()) {
        holder.textViewSubject.setTransitionName("SubjectName_"+position);
        holder.textViewShort.setTransitionName("SubjectShort_"+position);
        holder.textViewTeacher.setTransitionName("SubjectTeacher_"+position);
        holder.textViewRoom.setTransitionName("SubjectRoom_"+position);
        holder.frameLayoutSubjectColor.setTransitionName("SubjectColor_" + position);
    }

}


@Override
public int getItemCount() {
    return subjectList.size();
}


public class subjectViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    TextView textViewSubject,textViewShort, textViewRoom, textViewTeacher;
    FrameLayout frameLayoutSubjectColor;
    public subjectViewHolder(View itemView) {
        super(itemView);
        textViewSubject = (TextView) itemView.findViewById(R.id.textViewSubject);
        textViewShort = (TextView) itemView.findViewById(R.id.textViewShort);
        textViewRoom = (TextView) itemView.findViewById(R.id.textViewRoom);
        textViewTeacher = (TextView) itemView.findViewById(R.id.textViewTeacher);
        frameLayoutSubjectColor = (FrameLayout) itemView.findViewById(R.id.frameLayoutSubjectColor);
        itemView.setOnClickListener(this);


    }


    @Override
    public void onClick(View v) {
        if (clickListener != null) {
            clickListener.itemClicked(v, getPosition());
        }
    }
}

public interface ClickListener {
    public void itemClicked(View v, int postion);
}

public void setClickListener(ClickListener clickListener) {
    this.clickListener = clickListener;
}

public void removeItem(List<Subject> subjectList, int pos) {
    this.subjectList = subjectList;
    notifyItemRemoved(pos);
    System.out.println("removeItem");
}

public void updateItem(List<Subject> subjectList, int pos, int newPos) {
    this.subjectList = subjectList;
    notifyItemChanged(pos);
    if (pos != newPos) {
        notifyItemMoved(pos, newPos);
    }
    System.out.println("updateItem");
}

public void dataChange(List<Subject> subjectList) {
    this.subjectList = subjectList;
    notifyDataSetChanged();
    System.out.println("dataChange");
}
}

0 个答案:

没有答案