1

我正在尝试构建一个布局类似于新的Android Market,马赛克样式的活动,该活动列出了每个马赛克中的信息。当用户向左或向右滑动时,他们会看到越来越多的带有信息的马赛克。信息量是动态的,因为它是通过 AsyncTask 从 XML 文件加载的。

我决定使用 ViewPager/PagerAdapter 来处理视图和控制分页。每个页面都会有一个LinearLayout,类似于此链接中的图片:http: //dl.dropbox.com/u/4458251/mosaic.png

马赛克中的每个方块都是一个带有图像和文本视图的RelativeLayout。

我遇到的问题是显示的第一个视图始终为空白,当我尝试更改页面时,显示强制关闭错误。另外,当我调试时,我发现根本没有输入instantiateItem()方法PagesContentAdapter.java!是什么赋予了?。

这是我的活动(MosaicMenuActivity.java):

public class MosaicMenuActivity extends Activity {
public final String TAG = "NoniusDroidBaseApp";

// The title image (on top of the window)
private String TITLE_IMAGE = "title/title_MAIN";
// The background (normally you don't want to change it)
private String BACKAGROUND_IMAGE = "background";

// The content loader is used to load the application on another thread
// It makes sure that you can't interact with the application until the load
// is completed,
// but it will also blocks allows you to exit the application while it it
// loading.
// The interface will also never block
private PageContentLoader content_loader;

// The content adapter holds one application content array.
private PagesContentAdapter content_adapter;

// Actual system language
private String language;

/*
 * (non-Javadoc)
 * 
 * @see android.app.Activity#onCreate(android.os.Bundle)
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Apply the main.xml as the content view
    setContentView(R.layout.main);
    // Apply the Nonius default theme
    getApplicationContext().setTheme(R.style.Theme);
}

@Override
public void onResume() {
    super.onResume();

    // Whenever the application resumes, the loader check if the Theme
    // changed (or the language was modified)
    // If it was modified, it loads the theme again
    if (ThemeManager.themeModified(language)) {
        loadTheme();
    }

    // Get the actual system language
    language = SetTopBoxDetails.getLanguage();

    // Build the content adapter
    this.content_adapter = new PagesContentAdapter(
            this.getApplicationContext(), language);

    // Applies the content adapter to the content ViewPager
    ViewPager myPager = (ViewPager) findViewById(R.id.scrollview_pages);
    myPager.setAdapter(content_adapter);
    myPager.setCurrentItem(0);

    // Builds the module that will load the content from the xml
    this.content_loader = new PageContentLoader(this, content_adapter, language);
    // The content is read whenever the application resumes
    loadDynamicContent();
}


public boolean onKeyDown(int keyCode, KeyEvent event){
    Log.d(TAG, "onKeyDown");
    return super.onKeyDown(keyCode, event);
}

/*
 * This function is used to load content that comes from outside of the
 * application.
 */
private void loadDynamicContent() {
    // The content loader makes sure that the user can't interact with the
    // application until the load is done.
    this.content_loader.execute();
}

/*
 * Loads the application theme.
 */
private void loadTheme() {
    // Get the context
    Context context = this.getApplicationContext();

    // Load the background
    RelativeLayout relativelayout_base = (RelativeLayout) findViewById(R.id.relativelayout_base);
    relativelayout_base.setBackgroundDrawable(ThemeManager.getImage(
            BACKAGROUND_IMAGE, context));
}

AsyncTask 是由我的PageContentLoader.java. 我从 XML 中获取所有数据,但未显示:

public class PageContentLoader extends AsyncTask<Void, Integer, Boolean> {
    public static final String TAG = "PageContentLoader";
    private static int SLEEPING_TIME = 2000;

    // The location of the XML file
    private static final String XML_PATH = "/data/nonius/menu/mosaic.xml";

    // XML node keys used to parse xml
    private static final String KEY_PAGE = "page";
    private static final String KEY_PAGE_TEMPLATE = "template";
    private static final String KEY_TILE = "tile";

    // The parent activity. Used to lauch UI items
    private MosaicMenuActivity activity;

    // The content storage
    private PagesContentAdapter content_adapter;

    // The dialog that locks the user interaction
    private LoaderDialog dialog;

    // The array of content (in this demo, Pages!)
    public ArrayList<Page> pages;

    private String language;

    public PageContentLoader(MosaicMenuActivity activity,
            PagesContentAdapter content_adapter, String language) {
        this.activity = activity;
        this.pages = new ArrayList<Page>();
        this.dialog = new LoaderDialog(this.activity);
        this.content_adapter = content_adapter;
        this.language = language;
    }

    public boolean loadXml() {
        Log.d(TAG, "loadXml");
        // Clean the pages array
        this.pages = new ArrayList<Page>();

        // Get the DOM element
        Document doc = XmlHelper.getDomElement(XML_PATH);
        // If the document is null
        if (doc == null) {
            // Return fail
            return false;
        }

        // Get all the nodes with tag KEY_PAGE
        NodeList page_nodelist = doc.getElementsByTagName(KEY_PAGE);
        Log.d(TAG, "getElementsByTagName");

        // looping through all item nodes <item>
        for (int page_i = 0; page_i < page_nodelist.getLength(); page_i++) {
            Page page = new Page();

            Log.d(TAG, "Page " + page_i);
            // Get the element of the node list
            Element page_element = (Element) page_nodelist.item(page_i);

            String page_template = page_element.getAttribute(KEY_PAGE_TEMPLATE);
            page.setTemplate(page_template);

            NodeList tile_nodelist = page_element
                .getElementsByTagName(KEY_TILE);

            for (int tile_i = 0; tile_i < tile_nodelist.getLength(); tile_i++){
                // Create an empty tile
                Tile tile = new Tile();

                Element tile_element = (Element) tile_nodelist.item(tile_i);
                tile.importXmlElement(tile_element);

                page.addTile(tile);

                Log.d(TAG, tile.toString());
            }

            this.pages.add(page);
        }

        // Everything went ok
        return true;
    }

    protected void onPreExecute() {
        this.dialog.setLoadMode(activity.getString(R.string.loading_message));
        this.dialog.show();
    }

    protected Boolean doInBackground(Void... params) {
        // Parse the xml file
        return this.loadXml();
    }

    protected void onPostExecute(Boolean success) {
        // If the loader succeeded
        if (success) {
            // Update the adapter
            content_adapter.updateContent(pages);

            // If the dialog is visible, hide it
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
        // If the loader failed
        else {
            // Start the error dialog
            dialog.setErrorMode(activity
                .getString(R.string.loading_error_message));
        }
    }
}

PagerAdapter 在我的PageContentAdapter.java. 在进行一些调试之后,构造方法被成功调用,但创建页面内容的方法(instantiateItem())从未被调用:

public class PagesContentAdapter extends PagerAdapter {
public static final String TAG = "PagesContentAdapter";
// The list context
private Context context;

// The content array
public ArrayList<Page> pages;

// The system language
private String language;

/**
 * Class constructor
 * 
 * @param context
 *            The context used to build the ouput views
 */
public PagesContentAdapter(Context context, String language) {
    this.context = context;
    // Create an empty pages array
    this.pages = new ArrayList<Page>();
    this.language = language;
}

/**
 * Update the Adapter pages array and notify the parent
 * that the data was modified
 * @param pages
 */
public void updateContent(ArrayList<Page> pages) {
    this.pages = pages;
    //this.notifyDataSetChanged();
}

public int getCount() {
    return pages.size();
}

public Object getItem(int position) {
    return pages.get(position);
}

public long getItemId(int position) {
    return position;
}

@Override
public void destroyItem(View collection, int position, Object view) {
    ((ViewPager) collection).removeView((View) view);

}

@Override
public void finishUpdate(View arg0) {
    // TODO Auto-generated method stub

}

@Override
public Object instantiateItem(View page_view, int position) {
    Page page = new Page();
    page = pages.get(position);

    LayoutInflater page_layout_inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout linearlayout_page = (LinearLayout) page_layout_inflater.inflate(R.layout.page_template_5icons_v1, null);

    for(int i=0; i<5; i++) {
        linearlayout_page.addView(layoutTile(linearlayout_page, page, i));
    }

    ((ViewPager) page_view).addView(linearlayout_page, 0);

    return linearlayout_page;
}

public RelativeLayout layoutTile(LinearLayout linearlayout_page, Page page, int index) {
    RelativeLayout relativelayout_tile = null;

    switch(index) {
        case 0: relativelayout_tile = (RelativeLayout) linearlayout_page.findViewById(R.id.item_big_1);
        case 1: relativelayout_tile = (RelativeLayout) linearlayout_page.findViewById(R.id.item_small_1);
        case 2: relativelayout_tile = (RelativeLayout) linearlayout_page.findViewById(R.id.item_small_2);
        case 3: relativelayout_tile = (RelativeLayout) linearlayout_page.findViewById(R.id.item_small_3);
        case 4: relativelayout_tile = (RelativeLayout) linearlayout_page.findViewById(R.id.item_small_4);
    }

    // Inflate it in a layout xml
    LayoutInflater tile1_layout_inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    tile1_layout_inflater.inflate(R.layout.tile, relativelayout_tile);
    // Get the textview from the inflated layout
    TextView textview_tail = (TextView) relativelayout_tile.findViewById(R.id.textview_tile);
    Tile tile = page.getTiles().get(index);
    // Set its text
    textview_tail.setText(tile.getDescription());

    return relativelayout_tile;
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == object;
}

@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
    // TODO Auto-generated method stub

}

@Override
public Parcelable saveState() {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void startUpdate(View arg0) {
    // TODO Auto-generated method stub

}

}

感谢我能得到的任何帮助。感谢您抽出宝贵时间查看此问题!

4

0 回答 0