Implementing “Drilldown” navigation in Android App UI

前端 未结 3 473
轻奢々
轻奢々 2021-01-30 15:03

I am trying to learn how to do stuff in Android, and I\'m not sure of the best way to build the interface.

I\'ve been working on porting an iPhone app, which uses naviga

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-30 15:48

    So on an iphone when you say drill down I guess you mean when a user touches-up on a list row and it slides a new view on from the right, most of the time it has a nav bar at the top to give the user the option to go back?

    The way android handles this is simply by starting a new activity. So you would have your 'Books' ListActivity when a listItem is clicked you would define a new intent that starts your 'Chapters' ListActivity and so on. The nav bar at the top of an iphone is not standard UI in android as most people see the dedicated 'back' key as a way of getting back to the previews screen.

    This is how you start an intent in case you haven't seen it before:

    Intent chaptersIntent = new Intent(this, Chapters.class);
    this.startActivity(chaptersIntent);
    

    This article is worth a quick read through as it explains Activities perfectly

    http://d.android.com/guide/topics/fundamentals.html

    Also have a look at the android version of TableView - ListView:

    http://d.android.com/reference/android/widget/ListView.html

    and ListActivity:

    http://d.android.com/reference/android/app/ListActivity.html


    EDIT:: Sample Code I would do it something like this

    public class Books extends ListActivity {
    
    private String[] mBooks = new String[]{ "Book1", "Book2", "Book3", "Book4" };
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        ArrayAdapter booksAdapter = new ArrayAdapter(this, 
                android.R.layout.simple_list_item_1, 
                android.R.id.text1, 
                mBooks);
    
        this.setListAdapter(booksAdapter);
    
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
    
        Intent mViewChaptersIntent = new Intent(this, Chapters.class);
        mViewChaptersIntent.putExtra("BookName", mBooks[position]);
        startActivity(mViewChaptersIntent);
    }
    
    }
    

    So you pass through the id of the book as an extra to the Intent then in your Chapters Activity you get that extra in the onCreate method:

        @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        Bundle extras = getIntent().getExtras();
        if(extras != null) {
            String bookId = extras.getString("BookName");
        }
    }
    

    Finally make sure all new activities are added to your AndroidManifest.xml file:

    
    
    

    Hope that helps

提交回复
热议问题