[英]How to extend URL class to support other protocols in java (android)?
[英]how to extend a plain java class android?
我在正確設置復雜的列表視圖時遇到問題:我正在嘗試創建帶有滑動選項卡布局的兩級列表視圖,但是我的第二級列表不對正在執行的任何操作做出反應(例如,單擊列表中的項目)。
也許我需要擴展一個普通的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
}
}
文件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();
}
}
}
文件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);
}
}
文件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);
}
}
文件AnItemClickListener.java
package com.learn2crack.tab;
public interface AnItemClickListener {
void clickHappens(int i);
}
簡短的答案:您不是,班級是您在班級開始時說過的“班級”。 如果它們在外部並且與文件名匹配,則它們是外部類
如果要在片段和保存該片段的活動之間進行通信,請首先編寫一個接口; 您可以不用編寫一個代碼而擺脫困境,但這是一種很好的形式,可以幫助您組織代碼:
package com.yourcompany.appname.interfaces;
public interface AnItemClickListener
{
void clickHappens(int i);
}
請注意缺少方法主體,還請注意,由於我將其稱為接口,因此無需指定它們是抽象的,但它們是抽象的。 您可以添加更多方法,並且實現此方法的任何類都必須具有clickHappens方法,或者該方法本身必須是抽象的,其中包含具有該功能的類。
然后,編輯mainActivity的頂部說:'公共類MainActivity擴展FragmentActivity實現AnItemClickListener,HeadlinesFragment.OnHeadlineSelectedListener {',這意味着它具有為AnItemClickListener和HeadlinesFragment.OnHeadlineSelectedListener接口中的所有抽象方法提供主體的方法。 此時,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.
}
那不是很整潔嗎?
現在,您可以立即想到的兩種方式之一調用此函數:如果我們知道片段僅將作為該活動的一部分運行(不會被重用,這將破壞Fragments的目的之一)然后在我們的片段中我們可以說
MainActivity.this.clickHappens(pos); //where pos is the position
但這沒有使用界面。
更強大的是,由於我們在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");
}
}
}
因此,當您覆蓋onListItemClick時,只需說
callback.clickHappens( position );
並且您已經將點擊位置發送給了將“ this”傳遞給Fragment的活動。
告訴我其中的任何部分是否不清楚=]
當您做類似的事情時,更“正常”使用接口
Map map = new Hashmap<String, String>;
接着
map.get(s);
在這種情況下,盡管map是Map,但是您沒有調用Map的.get(),而Map只是基類,而是調用了HashMap的.get方法。
gl hf
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.