머리말 : 나는 Android에 매우 익숙합니다. 둘러 보았고 수많은 자습서를 읽었으며 아직 개념을 파악하지 못했습니다. 그것이 단순한 것이라면 나는 사전에 사과 할 것이다.FragmentPagerAdapter의 어려움
Android Studio 템플릿의 상용구 코드를 사용하여 FragmentPagerAdapter를 구현하는 활동이 있습니다. 내 목표는 플레이어 조각 (즉, 동일한 레이아웃, 각각 다른 점수)을 추적하기 위해 동일한 조각의 여러 인스턴스간에 전환하는 것입니다. 현재, 다양한 조각으로 보이는 것 사이를 전환 할 수 있습니다. 컴퓨터에서 첫 번째 조각의 단추를 클릭 할 수 있지만 다음 조각으로 전환하고 단추를 클릭하면 조각에서 아무 일도 발생하지 않지만 첫 번째 조각의 점수가 변경됩니다. 이것이 단 하나의 단편만을 사용하고 필요로하는 단편을 사용하지 않기 때문에 이것이라고 생각합니다. 점수를 각 프래그먼트에 저장하는 방법을 알아 내고 프래그먼트 2의 버튼이 프래그먼트 2의 점수 만 변경한다는 것을 알면 도움이 될 것입니다. 나는 그것을 함께 할 수있는 뭔가가 생각
public class SwipeTestFragment extends Fragment implements View.OnClickListener {
// The fragment argument representing the section number for this fragment.
private static final String ARG_SECTION_NUMBER = "section_number";
public SwipeTestFragment() {
}
// Returns a new instance of this fragment for the given section number.
public static SwipeTestFragment newInstance(int sectionNumber) {
SwipeTestFragment fragment = new SwipeTestFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setRetainInstance(true);
View rootView = inflater.inflate(R.layout.fragment_swipe, container, false);
//Set Page Title
TextView playerHeading = (TextView) rootView.findViewById(R.id.playerHeading);
String playerHeadingString = "Player " + getArguments().getInt(ARG_SECTION_NUMBER);
playerHeading.setText(playerHeadingString);
//This section sets up buttons.
ImageButton minusone = (ImageButton) rootView.findViewById(R.id.minusBtn1);
minusone.setOnClickListener(this);
ImageButton plusone = (ImageButton) rootView.findViewById(R.id.plusBtn1);
plusone.setOnClickListener(this);
return rootView;
}
@Override
public void onClick(View v) {
TextView score = (TextView) getActivity().findViewById(R.id.scoreSevenWonders);
TextView coins = (TextView) getActivity().findViewById(R.id.coinsSevenWonders);
switch (v.getId()) {
case R.id.minusBtn1:
String presentvaluestring = coins.getText().toString();
if (Integer.parseInt(presentvaluestring) > 0) {
int presentvalueint = Integer.parseInt(presentvaluestring);
presentvalueint--;
coins.setText(String.valueOf(presentvalueint));
if (presentvalueint % 3 == 2) {
setPlayerScore(-1, score);
}
}
break;
case R.id.plusBtn1:
presentvaluestring = coins.getText().toString();
int presetvalueint = Integer.parseInt(presentvaluestring);
presentvalueint++;
if ((presentvalueint % 3) == 0) {
setPlayerScore(1, score);
}
coins.setText(String.valueOf(presentvalueint));
break;
}
}
public TextView setPlayerScore(int increment, TextView total) {
String totalstring = total.getText().toString();
int totalint = Integer.parseInt(totalstring); //see if Integer.parseInt(total.getText().toString()) will work
totalint += increment;
total.setText(String.valueOf(totalint));
return total;
}
}
:
package com.brbecker.tabletopcompanion.Games;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import com.brbecker.tabletopcompanion.MainActivity;
import com.brbecker.tabletopcompanion.R;
public class SwipeActivity extends Activity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
// Supposed to be able to create multiple fragment instances and get at them again later.
private SwipeTestFragment player1, player2, player3, player4, player5, player6, player7;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipe);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_swipe, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
그런 다음 FragmentPagerAdapter 클래스 다음
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a SwipeTestFragment.
return SwipeTestFragment.newInstance(position + 1);
}
@Override
public int getCount() {
// Show 7 total pages.
return 7;
}
// This method is supposed to save a reference to a created Fragment.
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment createdFragment = (Fragment) super.instantiateItem(container, position);
switch (position) {
case 0:
player1 = (SwipeTestFragment) createdFragment;
break;
case 1:
player2 = (SwipeTestFragment) createdFragment;
break;
case 2:
player3 = (SwipeTestFragment) createdFragment;
break;
case 3:
player4 = (SwipeTestFragment) createdFragment;
break;
case 4:
player5 = (SwipeTestFragment) createdFragment;
break;
case 5:
player6 = (SwipeTestFragment) createdFragment;
break;
case 6:
player7 = (SwipeTestFragment) createdFragment;
break;
}
return createdFragment;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "PLAYER 1";
case 1:
return "PLAYER 2";
case 2:
return "PLAYER 3";
case 3:
return "PLAYER 4";
case 4:
return "PLAYER 5";
case 5:
return "PLAYER 6";
case 6:
return "PLAYER 7";
}
return null;
}
}
조각 위치 코드 : 여기
는 내가 지금까지 무엇을 가지고 getItem() 메서드를 사용했지만 확실하지 않습니다. 필요한 경우 추가 코드를 제공하는 것보다 기쁘게 생각합니다.
경우 뭔가 미상에 잘 모르겠지만, 많은 버튼과 텍스트 뷰를위한 코드를 잘라 냈습니다. – brbecker87