简体   繁体   English

如何扩展一个普通的Java类Android?

[英]how to extend a plain java class android?

I am having problems properly setting up a complex listview: I am trying to create a two-level listview with a swipe tab layout, but my second level does not react to any actions taking place (eg clicking on an item in the list). 我在正确设置复杂的列表视图时遇到问题:我正在尝试创建带有滑动选项卡布局的两级列表视图,但是我的第二级列表不对正在执行的任何操作做出反应(例如,单击列表中的项目)。

Perhaps I need to extend a plain java file class but I am unsure how to do this. 也许我需要扩展一个普通的Java文件类,但是我不确定如何做到这一点。 Suggestions? 有什么建议吗?

File MainActivity.Java 文件MainActivity.Java

  package com.learn2crack.tab;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity implements AnItemClickListener, HeadlinesFragment.OnHeadlineSelectedListener {
    ViewPager Tab;
    TabPagerAdapter TabAdapter;
    ActionBar actionBar;

    public void clickHappens(int position){
        //whatever you need to do in response to a click knowing what position in the 
        //data set correlated to the row that was clicked.
         // The user selected the headline of an article from the HeadlinesFragment

        // Capture the article fragment from the activity layout
        ArticleFragment articleFrag = (ArticleFragment)
                getSupportFragmentManager().findFragmentById(R.id.article_fragment);

        if (articleFrag != null) {
            // If article frag is available, we're in two-pane layout...

            // Call a method in the ArticleFragment to update its content
            articleFrag.updateArticleView(position);

        } else {
            // If the frag is not available, we're in the one-pane layout and must swap frags...

            // Create fragment and give it an argument for the selected article
            ArticleFragment newFragment = new ArticleFragment();
            Bundle args = new Bundle();
            args.putInt(ArticleFragment.ARG_POSITION, position);
            newFragment.setArguments(args);
            android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

            // Replace whatever is in the fragment_container view with this fragment,
            // and add the transaction to the back stack so the user can navigate back
            transaction.replace(R.id.fragment_container, newFragment);
            transaction.addToBackStack(null);

            // Commit the transaction
            transaction.commit();

        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TabAdapter = new TabPagerAdapter(getSupportFragmentManager());

        Tab = (ViewPager)findViewById(R.id.pager);
        Tab.setOnPageChangeListener(
                new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {

                        actionBar = getActionBar();
                        actionBar.setSelectedNavigationItem(position);                    }
                });
        Tab.setAdapter(TabAdapter);

        actionBar = getActionBar();
        //Enable Tabs on Action Bar
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ActionBar.TabListener tabListener = new ActionBar.TabListener(){

            @Override
            public void onTabReselected(android.app.ActionBar.Tab tab,
                    FragmentTransaction ft) {
                // TODO Auto-generated method stub

            }

            @Override
             public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {

                Tab.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(android.app.ActionBar.Tab tab,
                    FragmentTransaction ft) {
                // TODO Auto-generated method stub

            }};
            //Add New Tab
            actionBar.addTab(actionBar.newTab().setText("Software Search").setTabListener(tabListener));
            actionBar.addTab(actionBar.newTab().setText("Maps").setTabListener(tabListener));
            actionBar.addTab(actionBar.newTab().setText("Scanner").setTabListener(tabListener));

    }

@Override
public void onArticleSelected(int position) {
    // TODO Auto-generated method stub

}


}

File SoftWareSearchActivity.java 文件SoftWareSearchActivity.java

package com.learn2crack.tab;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;

interface OnHeadlineSelectedListener {
    /** Called by HeadlinesFragment when a list item is selected */
    public void onArticleSelected(int position);
}

public class SoftWareSearchActivity extends FragmentActivity 
        implements HeadlinesFragment.OnHeadlineSelectedListener {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_articles);

        // Check whether the activity is using the layout version with
        // the fragment_container FrameLayout. If so, we must add the first fragment
        if (findViewById(R.id.fragment_container) != null) {

            // However, if we're being restored from a previous state,
            // then we don't need to do anything and should return or else
            // we could end up with overlapping fragments.
            if (savedInstanceState != null) {
                return;
            }

            // Create an instance of ExampleFragment
            HeadlinesFragment firstFragment = new HeadlinesFragment();

            // In case this activity was started with special instructions from an Intent,
            // pass the Intent's extras to the fragment as arguments
            firstFragment.setArguments(getIntent().getExtras());

            // Add the fragment to the 'fragment_container' FrameLayout
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, firstFragment).commit();
        }
    }

    public void onArticleSelected(int position) {
        // The user selected the headline of an article from the HeadlinesFragment

        // Capture the article fragment from the activity layout
        ArticleFragment articleFrag = (ArticleFragment)
                getSupportFragmentManager().findFragmentById(R.id.article_fragment);

        if (articleFrag != null) {
            // If article frag is available, we're in two-pane layout...

            // Call a method in the ArticleFragment to update its content
            articleFrag.updateArticleView(position);

        } else {
            // If the frag is not available, we're in the one-pane layout and must swap frags...

            // Create fragment and give it an argument for the selected article
            ArticleFragment newFragment = new ArticleFragment();
            Bundle args = new Bundle();
            args.putInt(ArticleFragment.ARG_POSITION, position);
            newFragment.setArguments(args);
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

            // Replace whatever is in the fragment_container view with this fragment,
            // and add the transaction to the back stack so the user can navigate back
            transaction.replace(R.id.fragment_container, newFragment);
            transaction.addToBackStack(null);

            // Commit the transaction
            transaction.commit();
        }
    }
}

File HeadlinesFragment.java 文件HeadlinesFragment.java

package com.learn2crack.tab;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class HeadlinesFragment extends ListFragment {
    OnHeadlineSelectedListener mCallback;


    // The container Activity must implement this interface so the frag can deliver messages
    public interface OnHeadlineSelectedListener {
        /** Called by HeadlinesFragment when a list item is selected */
        public void onArticleSelected(int position);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // We need to use a different list item layout for devices older than Honeycomb
        int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
                android.R.layout.simple_list_item_activated_1 : android.R.layout.simple_list_item_1;

        // Create an array adapter for the list view, using the Ipsum headlines array
        setListAdapter(new ArrayAdapter<String>(getActivity(), layout, Ipsum.Headlines));
    }

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

        // When in two-pane layout, set the listview to highlight the selected list item
        // (We do this during onStart because at the point the listview is available.)
        if (getFragmentManager().findFragmentById(R.id.article_fragment) != null) {
            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception.
        try {
            mCallback = (OnHeadlineSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // Notify the parent activity of selected item
        mCallback.onArticleSelected(position);

        // Set the item as checked to be highlighted when in two-pane layout
        getListView().setItemChecked(position, true);
    }
}

File ArticleFragment.java 文件ArticleFragment.java

package com.learn2crack.tab;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ArticleFragment extends Fragment {
    final static String ARG_POSITION = "position";
    int mCurrentPosition = -1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {

        // If activity recreated (such as from screen rotate), restore
        // the previous article selection set by onSaveInstanceState().
        // This is primarily necessary when in the two-pane layout.
        if (savedInstanceState != null) {
            mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
        }

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.article_view, container, false);
    }

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

        // During startup, check if there are arguments passed to the fragment.
        // onStart is a good place to do this because the layout has already been
        // applied to the fragment at this point so we can safely call the method
        // below that sets the article text.
        Bundle args = getArguments();
        if (args != null) {
            // Set article based on argument passed in
            updateArticleView(args.getInt(ARG_POSITION));
        } else if (mCurrentPosition != -1) {
            // Set article based on saved instance state defined during onCreateView
            updateArticleView(mCurrentPosition);
        }
    }

    public void updateArticleView(int position) {
        TextView article = (TextView) getActivity().findViewById(R.id.article);
        article.setText(Ipsum.Articles[position]);
        mCurrentPosition = position;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        // Save the current article selection in case we need to recreate the fragment
        outState.putInt(ARG_POSITION, mCurrentPosition);
    }
}

File AnItemClickListener.java 文件AnItemClickListener.java

package com.learn2crack.tab;

public interface AnItemClickListener {

    void clickHappens(int i);

}

short answer: you don't, classes are classes b/c you said 'class' at the beginning of the class; 简短的答案:您不是,班级是您在班级开始时说过的“班级”。 they are outer classes if they are outer and match the file name 如果它们在外部并且与文件名匹配,则它们是外部类

if you want to communicate between a fragment and the activity that is holding it, first write an interface; 如果要在片段和保存该片段的活动之间进行通信,请首先编写一个接口; u can get away with not writing one, but it's good form and helps organize your code: 您可以不用编写一个代码而摆脱困境,但这是一种很好的形式,可以帮助您组织代码:

package com.yourcompany.appname.interfaces;

public interface AnItemClickListener
{
void clickHappens(int i);
}

note the lack of a method body, also note that since I'm calling this an interface, I don't need to specify that these are abstract, but they are. 请注意缺少方法主体,还请注意,由于我将其称为接口,因此无需指定它们是抽象的,但它们是抽象的。 You can add more methods, and any class that implements this must have a clickHappens method or itself be abstract, containing classes that do. 您可以添加更多方法,并且实现此方法的任何类都必须具有clickHappens方法,或者该方法本身必须是抽象的,其中包含具有该功能的类。

Then you edit the top of your mainActivity to say: 'public class MainActivity extends FragmentActivity implements AnItemClickListener, HeadlinesFragment.OnHeadlineSelectedListener {', meaning it has methods that provide the body for all the abstract methods in both the AnItemClickListener and the HeadlinesFragment.OnHeadlineSelectedListener interfaces. 然后,编辑mainActivity的顶部说:'公共类MainActivity扩展FragmentActivity实现AnItemClickListener,HeadlinesFragment.OnHeadlineSelectedListener {',这意味着它具有为AnItemClickListener和HeadlinesFragment.OnHeadlineSelectedListener接口中的所有抽象方法提供主体的方法。 At this point you IDE should be throwing an error and telling you that you need to implement an abstract function, so put the following method in this class: 此时,IDE应该抛出一个错误并告诉您需要实现一个抽象函数,因此请将以下方法放在此类中:

public void clickHappens(int position){
    //whatever you need to do in response to a click knowing what position in the 
    //data set correlated to the row that was clicked.
}

isn't that neat-o? 那不是很整洁吗?

now, you can call this function in one of two ways that come immediatly to mind: if we know the fragment is only ever going to be running as a part of this activity( not being reused, which defeats one of the purposes for Fragments ) then in our fragment we can say 现在,您可以立即想到的两种方式之一调用此函数:如果我们知道片段仅将作为该活动的一部分运行(不会被重用,这将破坏Fragments的目的之一)然后在我们的片段中我们可以说

MainActivity.this.clickHappens(pos); //where pos is the position

but this isn't using the interface. 但这没有使用界面。

more robustly, since we pass this to the Fragment during onAttach, we make sure a callback can be cast correctly in onAttach like so: 更强大的是,由于我们在onAttach期间将其传递给Fragment,因此我们确保可以在onAttach中正确地进行回调,如下所示:

class aFragment extends ListFragment{
Activity callback;

@Override
onAttach{
    try {
            callback = (AnItemClickListener) activity; //look familiar?
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + 
                                     " must implement AnItemClickListener");
        }
    }
}

so when you override onListItemClick, just say 因此,当您覆盖onListItemClick时,只需说

callback.clickHappens( position );

and you've sent the clicked position to the activity that passed 'this' to the Fragment. 并且您已经将点击位置发送给了将“ this”传递给Fragment的活动。

tell me if any part of that isn't clear =] 告诉我其中的任何部分是否不清楚=]

a more "normal" use of interfaces is when you do something like 当您做类似的事情时,更“正常”使用接口

Map map = new Hashmap<String, String>;

and then 接着

map.get(s);

in that case, though map is a Map, you aren't calling .get() of Map--Map is only the base class--you are calling the method .get of HashMap. 在这种情况下,尽管map是Map,但是您没有调用Map的.get(),而Map只是基类,而是调用了HashMap的.get方法。

gl hf gl hf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM