标签应用开发

时间:2011-07-11 08:31:11

标签: android tabs tabactivity

我希望在Android中创建一个标签应用程序。 我希望我的应用程序中基本上有四个选项卡。 即,Home,Compose,Inbox,Sent。 我开始使用TabContainer扩展TabActivity,这是一种正确的方法。 由于我将使用MenuItem,我使用的是收件箱的多个视图,如收件箱列表,我可以阅读,然后删除所有。 我想知道我该如何处理? 基本上我希望创建一个类似于gmail的应用程序。

任何想法或方向都可能对我有帮助

public class Inbox extends ListActivity{
    private ProgressDialog progressDialog = null; 
    private ArrayList<EmailElement> emailElement = null;
    private InboxAdapter inboxAdapter;
    private CheckBoxWithInboxList newInboxAdapter;
    private Runnable beforeFetchingEmail;
    private TextView emailFromTextView = null;
    private TextView emailSubjectTextView = null;
    ListView listView;
    private static int mode = 0;
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.inbox);

        mode = this.getIntent().getIntExtra("EDIT_MODE", -1);
        Log.i("EDITMODE", String.valueOf(mode));
        emailElement = new ArrayList<EmailElement>();
        if(mode ==0 || mode==-1){
         this.inboxAdapter = new InboxAdapter(this, R.layout.inbox_row, emailElement);
         setListAdapter(inboxAdapter);
        }else{
            this.newInboxAdapter = new CheckBoxWithInboxList(this, R.layout.inbox_row, emailElement);
            setListAdapter(newInboxAdapter);
        }
        beforeFetchingEmail = new Runnable() {
            @Override
            public void run() {
                    getEmails();
                }
            };
            Thread thread =  new Thread(null, beforeFetchingEmail, "MagentoBackground");
            thread.start();
            progressDialog = ProgressDialog.show(Inbox.this,"Please wait...",
                    "Retrieving Emails ...", true);
            listView = getListView();
            listView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parentView, View view,
                        int position, long id) {

                    }
                });
}
 private Runnable returnRes = new Runnable() {

        @Override
        public void run() {
            if(emailElement != null && emailElement.size() > 0){
                if(mode==0 || mode==-1){
                    inboxAdapter.notifyDataSetChanged();
                    for(int i=0;i<emailElement.size();i++){
                        inboxAdapter.add(emailElement.get(i));
                    }
                    progressDialog.dismiss();
                    inboxAdapter.notifyDataSetChanged();
                }else{
                    newInboxAdapter.notifyDataSetChanged();
                    for(int i=0;i<emailElement.size();i++){
                        newInboxAdapter.add(emailElement.get(i));
                    }
                    progressDialog.dismiss();
                    newInboxAdapter.notifyDataSetChanged();
                }

            }
        }
    };
 private void getEmails(){
     try{
         emailElement = new ArrayList<EmailElement>();
         EmailElement ee0 = new EmailElement();
         ee0.setFrom("Robin Thapa");
         ee0.setSubject("Urgent Meeting");
         emailElement.add(ee0);
         EmailElement ee1 = new EmailElement();
         ee1.setFrom("Deepak Thapa");
         ee1.setSubject("Staff meeting @Sunday");
         emailElement.add(ee1);
         Thread.sleep(5000);
     }catch(Exception ex){

     }
     runOnUiThread(returnRes);
 }
 @Override
 public boolean onCreateOptionsMenu(Menu menu){
       MenuInflater menuInflater = getMenuInflater();
       menuInflater.inflate(R.menu.inbox_menu, menu);
       return true;
   }
 @Override
  public boolean onOptionsItemSelected(MenuItem item){
       switch(item.getItemId()){
       case R.id.inboxEdit:
               Intent intent = new Intent(this,EmailClient.class);
               intent.putExtra("tabId", 2);
               intent.putExtra("EDIT_MODE", 1);
               startActivityForResult(intent, Intent.FILL_IN_DATA);
           return true;
       case R.id.inboxRefresh:
           return true;
       case R.id.inboxNext:
           return true;
       case R.id.inboxPrevious:
           return true;
        }
       return false;
}
private class InboxAdapter extends ArrayAdapter<EmailElement>{
     private ArrayList<EmailElement> items;

     public InboxAdapter(Context context,int textViewResourceId, 
                ArrayList<EmailElement> items) {
        super(context, textViewResourceId, items);
        this.items = items;
    }
      @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                View view = convertView;
                if (view == null) {
                    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    view = vi.inflate(R.layout.inbox_row, null);
                }
                EmailElement o = items.get(position);
                if (o != null) {
                    CheckBox emailCheckBox = (CheckBox)view.findViewById(R.id.inboxCheckBoxId);
                    emailCheckBox.setVisibility(View.INVISIBLE);
                        emailFromTextView = (TextView) view.findViewById(R.id.inboxEmailFrom);
                        emailSubjectTextView = (TextView) view.findViewById(R.id.inboxEmailSubject);
                         if (emailFromTextView != null){
                             emailFromTextView.setText("From: "+o.getFrom());
                        }
                        if(emailSubjectTextView != null){
                            emailSubjectTextView.setText("Sub: ["+ o.getSubject()+"]");
                        }
                }
                return view;
        }
 }


private class CheckBoxWithInboxList extends ArrayAdapter<EmailElement>{
 private ArrayList<EmailElement> items;
 public CheckBoxWithInboxList(Context context,int textViewResourceId, 
            ArrayList<EmailElement> items) {
    super(context, textViewResourceId, items);
    this.items = items;
}
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
         View view = convertView;
         if (view == null) {
             LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             view = vi.inflate(R.layout.inbox_row, null);
         }
         EmailElement o = items.get(position);
         if (o != null) {
            CheckBox emailCheckBox = (CheckBox)view.findViewById(R.id.inboxCheckBoxId);
            emailCheckBox.setVisibility(View.VISIBLE);
                emailFromTextView = (TextView) view.findViewById(R.id.inboxEmailFrom);
                 emailSubjectTextView = (TextView) view.findViewById(R.id.inboxEmailSubject);
                  if (emailFromTextView != null){
                     emailFromTextView.setText("From: "+o.getFrom());
                 }
                 if(emailSubjectTextView != null){
                    emailSubjectTextView.setText("Sub: ["+ o.getSubject()+"]");
                 }
         }
         return view;
 }

} }

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

您可能希望阅读此Tab Layout | Android Development

这是一个很好的起点